Linked List Cycle 4 - Delete cycle

Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. Return true if there is a cycle in the linked list. Otherwise, return false. Examples Example 1 Input: head = [0, 1, 3, 2, 4] output: [0, 1, 3, 2, 4] Explanation: Cycle starts at node 2 ...

Convert BST to Greater Sum Tree

Problem Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. Examples Example 1: ...

Longest Absolute File Path Problem

Problem Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture: ...

Implement 3 stacks in 1 array

Problem Implement 3 stacks in 1 array Solution Method 1 - Create 3 Stacks with Fixed Max Size Divide the array in three equal parts and allow the individual stack to grow in that limited space (note: [ means inclusive, while ( means exclusive of the end point). for stack 1, we will use [0, n/3) for stack 2, we will use [n/3, 2n/3) for stack 3, we will use [2n/3, n) Code Java int stackSize = 300; int[] buffer = new int[stackSize * 3]; int[] stackPointer = {0,0,0}; // stack pointers to track top element void push(int stackNum, int value) { /* Find the index of the top element in the array + 1, and increment the stack pointer */ int index = stackNum * stackSize + stackPointer[stackNum] + 1; stackPointer[stackNum]++; buffer[index] = value; } int pop(int stackNum) { int index = stackNum * stackSize + stackPointer[stackNum]; stackPointer[stackNum]--; int value = buffer[index]; buffer[index] = 0; return value; } int peek(int stackNum) { int index = stackNum * stackSize + stackPointer[stackNum]; return buffer[index]; } boolean isEmpty(int stackNum) { return stackPointer[stackNum] == stackNum * stackSize; } ...

List Combinations - Generate unique combinations from list selecting 1 element from each

Problem Given a list of lists containing elements, write a function that prints out the permutations of of the elements such that, each of the permutation set contains only 1 element from each list and there are no duplicates in the list of permutation sets. Examples Example 1: Input: lists = [ [a1, b1, c1], [a2, b2] ] Output: [ [a1, a2], [a1, b2], [b1, a2], [b1, b2], [c1, a2], [c1, b2] ] Explanation: Note that [a1, a2] is same as [a2, a1] in terms of combination, though they are separate permutation. ...

Smallest subarray with Sum exactly equal to K

Problem This problem is variant of Minimum Size Subarray Sum Problem Solution Method 1 - Brute Force This is similar to the brute force approach we discussed for the original problem. The only difference is that we’re checking if the subarray sum is equal to K instead of greater than or equal to K. Code Java public class Solution { private static int minSubArrayLenWithSumK(int[] a, int K) { int n = a.length; int ans = Integer.MAX_VALUE; for(int i = 0; i < n; i++) { int currSubarraySum = 0; for(int j = i; j < n; j++) { currSubarraySum += a[j]; if(currSubarraySum == K) { ans = Math.min(ans, j-i+1); break; } if(currSubarraySum > K) { break; } } } return ans == Integer.MAX_VALUE ? 0 : ans; } } ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy