I am struggling with this part of my task at work. I’ve deliberately not detailed the context of the work task to try and keep the focus on the problem. I have to merge rectangles into a single polygon as shown in the attached image, but I need the list of points so that I can write these out into
Tag: algorithm
detect last foreach loop iteration
Supposing that I have some foreach loop like this: Is there a way to check inside foreach that the actual name is the last one in Set without a counter? I didn’t found here some question like this. Answer There isn’t, take a look at How does the Java ‘for each’ loop work? You must change your loop to use
How to merge 3 sorted arrays into 1 sorted array in Big-O(N) time?
Trying to merge 3 arrays into one so that the final array is in order. Given Merge the arrays so that the final array d = {1,1,2,3,4,5} Can’t just concatenate them and then sort the d array because that would make the time complexity larger than Big-O(N). This is what I got so far. Having problems with indexes out of
Heapsort Within a Given Range
I am trying to write a Heapsort method that only performs the sort within a given range passed into the method. The ranges low and high are passed in and these values correspond to values inside the heap, not indices of the heap. For example, the input array might be: 28 10 49 20 59 61 17 and if low
Depth first search in finding a path in matrix
My understanding about dfs is using stack (bfs using queue). However, if i want to traversal a matrix in dfs. How do I suppose to do that? Suppose I have a matrix and I want to find a path start from top left to bottom right, and it can only move down and right. Above is an online version a
A zero-indexed array given & An equilibrium index of this array [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last month. Improve this question A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such
Count total subsequences whose sum is divisible by k
I am trying to write a DP solution for the problem: count total number of sub-sequences possible of an array whose elements’ sum is divisible by k. I have written the following solution. But it is not giving the correct result. Like in the following code snippet, the array is {1, 2, 1} and k = 3. So expected total
Connect 4 check for a win algorithm
I know there is a lot of of questions regarding connect 4 check for a win. The issue is that most of other algorithms make my program have runtime errors, because they try to access an index outside of my array. My algorithm is like this: count is the variable that checks for a win if count is equal or
Given n and k, return the kth permutation sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3 ) : “123” “132” “213” “231” “312” “321” Given n and k, return the kth permutation sequence. For example, given n = 3, k = 4, ans = “231”. There
How to get Cartesian product from multiple lists?
Say I have several List<T>s, I will put them into another list or other collections, so I don’t know how many list<T> I have until I call List<List<T>>.size() Take below List<Integer> as an example: How can I get the result of list1*list2*list3*…listn as a Cartesian product? For example: should be: Answer You can use recursion to achieve it, your base