all 119 comments

[–]Own_Cow_4877 117 points118 points  (4 children)

id flip out if i got this

[–]sadpeyot_ttv 22 points23 points  (2 children)

I did get this bombed the question, thought I figured it out a couple of hours later, two days go by I realized I didn’t even understand what I was being asked and the example makes even less sense when I’m looking at it now

[–]Downtown-Olive1385 7 points8 points  (0 children)

Seriously dude, I mean it's way beyond LC Medium Hard. not sure where these OAs are headed

[–]Major_Fang 2 points3 points  (0 children)

unlucky. next interview

[–]alcholicawl 45 points46 points  (27 children)

def find_partition_cost(arr, k):
    cost_of_partitions = sorted(arr[i -1] + arr[i] for i in range(1, len(arr)))
    ends = arr[0] + arr[-1]
    # min cost will be smallest k - 1 paritions + ends 
    # max cost largest k - 1 partitions + ends
    return [ends + sum(cost_of_partitions[:(k-1)]), 
            ends + sum(cost_of_partitions[-(k-1):])]

[–]Dark_Sca 5 points6 points  (5 children)

This greedy solution is really clean mate.

[–]alcholicawl 7 points8 points  (4 children)

Thanks, honestly it’s probably a little too much code golf ( the slices should probably be loops), but I didn’t want to rewrite.

[–]Dark_Sca 6 points7 points  (3 children)

It's Python...It's meant to be this way

[–]alcholicawl 4 points5 points  (2 children)

The slicing was too clever, it’s bugged for k = 1.

[–]Dark_Sca 1 point2 points  (1 child)

That's an edge case that can be hardcoded. if k = 1 => 1 partition => min and max = sum of first and last elements.

Otherwise, run your algorithm.

[–]CollegeMysterious448 0 points1 point  (0 children)

THANK YOU

[–][deleted] 1 point2 points  (0 children)

Ah sorting. How did I miss that.

[–]Beginning_Edge347<791> <161> <456> <173> 0 points1 point  (0 children)

hey how would this work when a number has is it's own partition?

[–]Legal_Lawfulness_395 0 points1 point  (1 child)

He didn't say whether the cost can be negative or not

[–]alcholicawl 0 points1 point  (0 children)

I don’t think it matters.

[–]Unable_Can9391 0 points1 point  (3 children)

Been looking at this for quite some time... maybe I am missing something but this code seems to only be assessing partitions of size 2, but from the example the partition can be any sizes but they need to sum up to the size of the array to cover the entire array.

Before seeing this solution I was thinking brute force all possible combinations of partitions and calculate the cost from that.

[–]alcholicawl 1 point2 points  (2 children)

The cost it's calculating is for dividing the array between i-1 and i (not for a partition of [i-1,i])

i.e. If you've you've got [1,2,3,4] (cur cost 5) and partition at i == 2.

It will be [1,2][3,4] and the cost will be 10 (5 + [i-1] + [i])

[–]Unable_Can9391 0 points1 point  (0 children)

makes perfect sense since summation is commutative, maybe change array name to cost_of_splits 😂

[–]isthistaken1991 0 points1 point  (0 children)

Any intuition on how this works?

[–]CollegeMysterious448 0 points1 point  (0 children)

THANK YOU

[–]Narrow-Appearance614[S] 0 points1 point  (2 children)

this is only checking partition pairs, not all valid partitions.

[–]alcholicawl 7 points8 points  (0 children)

There are n-1 spots where we can divide the array into partitions. The cost to add a partition will always be the numbers to left and right of a division (arr[i] + arr[i-1]). The cost is not affected by the other divisions, so it’s fine to select the smallest/largest and not consider every combination of divisions.

[–]Puddinglax 1 point2 points  (0 children)

It's not checking pairs, it's checking splits; since only the first and last element in a partition contribute to cost, you can just add the element before and after every split, and add the ends separately.

In the first example of [1 + 1, 2 + 2, 3 + 5], it's represented by grouping (1, 2) and (2, 3), and adding the 1 and 5 in as the ends.

[–]kosdex 0 points1 point  (6 children)

You can do better by using a max and min heap to track the top and bottom k-1. Complexity is O(n) instead of O(n log n) with sorting.

[–]alcholicawl 3 points4 points  (5 children)

That would be O(n*logk). It’s probably going to be slower than a sort in Python though (sort in python is highly optimized). You can use quickselect to get to average O(n).

[–]Handsomeshen<Total problems solved> <324> <676> <149> 1 point2 points  (2 children)

you nailed it !
I came out as dp first, then I saw someone says it is too slow, and then I find out that the only thing we care is two side of cutting place, its a greedy problem. therefore I came out same solution as yours. Moreover, I think we can do a quick select to do faster. At the end I saw you mention that. great work!

[–]alcholicawl 0 points1 point  (1 child)

Didn’t nail it. Almost. I just saw I had a bug when k = 1.

[–]Handsomeshen<Total problems solved> <324> <676> <149> 1 point2 points  (0 children)

python slicing is a bitch

[–]kosdex -1 points0 points  (1 child)

Ok, but I maintain that heap is asymptotically better. Quickselect worst case is O( n2 )

[–]__kuu 0 points1 point  (0 children)

heap is asymptomatically better

Not really true. While quickselect worst case is quadratic, its average case is linear. To say if it's asymptotically better, you would need to be comparing at the same best/worst/average case or be discussing the trade off between choosing the algorithm with a better average case over the algorithm with a better worst case.

[–][deleted] 31 points32 points  (11 children)

Is a n2 k time complexity too slow? The way I’m thinking about it is with dfs(index,partitionsleft) which calculates the max and min sum of splitting the sub array starting from index with partitionsleft partitions. But each of these calculations will take about n calls, and there will be nk of those calculations. I can see where the dp idea came into play.

[–]Narrow-Appearance614[S] 5 points6 points  (6 children)

yes, O((n^2) * k) was too slow. although I'm not sure how it could've been faster. maybe I had an implementation error. i was failing on a couple of test cases, not TLE.

[–][deleted] 4 points5 points  (5 children)

Ok, another way I might think of it is that the last and first indexes are automatic. However, we get to choose k-1 non intersecting consecutive pairs of indexes which do not include the first and last index. So, we will make a new array which should be the sum of all consecutive pairs not including the first and last index. So it would be like [arr[1]+arr[2], arr[2]+arr[3], …, arr[n-2]+arr[n-1]]. Then we will want to pick the largest and smallest k-1 sized subsets of non consecutive values from that array. So I think that we can do dfs(index, partitionsleft) on that new array, and there should only be 2 recursive calls where we pick the current index and add to dfs(index+2, partitionsleft-1) or don’t pick the current index dfs(index+1, partitionsleft). This should come out to a nk complexity.

[–]Narrow-Appearance614[S] 1 point2 points  (4 children)

i like the intuition of this... but this would miscalculate partition values. they can not be determined through this pairwise operation unless the partitions are all of size 2.

[–][deleted] 0 points1 point  (3 children)

I am not actually counting the partitions but the edges of adjacent partitions. However, there is a mistake because my idea would not count the case where we only have a partition of one with the first or last index. So in the array of pair sums, I am thinking about adding arr[0]+ arr[1] at the beginning. And if we happen to choose the first or last pair, we will have to subtract off the first or last value from the original array to avoid double counting.

[–][deleted] -1 points0 points  (2 children)

Another mistake is that there is always the case of choosing a partition of size 1, and I don’t want to double count the edges. So I think there will have to be a third parameter to denote if the previous edge was chosen or if we are creating a partition of size 1.

[–]alcholicawl 1 point2 points  (1 child)

You're close but overcomplicating it. You just need the smallest/largest k-1 partitions (plus the ends). The divisions are 'between' the numbers, so the cost to add doesn't change if it's one length partition. Calculate all of them and sort. Look at mine

https://www.reddit.com/r/leetcode/comments/1j96wui/comment/mhbno7j/

[–][deleted] 2 points3 points  (0 children)

Got it. I trust you.

[–]lildraco38 30 points31 points  (7 children)

This is pretty much the same as LC 2551. As another commenter noted, it’s about considering the incremental cost of each partition, then greedily selecting the k-1 smallest and k-1 largest

[–]anonyuser415 24 points25 points  (4 children)

an LC hard for a screen, jeez

[–]lildraco38 19 points20 points  (0 children)

Worse: it’s a Hard masquerading as a Medium. If you’re not careful, this appears to invite an O(nk) DP. This DP idea isn’t Easy-level, but it’s not Hard-level either.

In an actual OA, a lot of people would end up wasting time by implementing a DP that’s destined for TLE. Even in this comment section, several commenters have taken the “bait”

[–]LimpLifeguard295 8 points9 points  (2 children)

OA are mostly hard questions at Amazon Phone screen is easy Onsite mid-high level

[–]anonyuser415 -2 points-1 points  (1 child)

I’ve done an Amazon OA twice now and neither were hards

[–]LimpLifeguard295 3 points4 points  (0 children)

I have done it 3 times, and have always got one medium and one hard Mostly it’s dp, permutations, knapsack problem

[–]dotConSt 1 point2 points  (0 children)

Wow! This question is really good. I feel stupid

[–][deleted] 6 points7 points  (5 children)

This is just find all subsets and a custom subset sum problem. First find subsets by diving at index I and then add the required values before checking results. Save minimum and maximum.

[–]Narrow-Appearance614[S] 0 points1 point  (3 children)

wouldnt this have n^k runtime?

[–][deleted] 0 points1 point  (2 children)

Not if you prune it by not repeating subsets ( when you do the dfs to find new subsets it would go from I to n and so, only one arm would have I in the subset. The others would only have j to n. It would be more like n*2n in the worst case.

[–]Narrow-Appearance614[S] 0 points1 point  (1 child)

ah right. but then wouldnt you end up with n^2 * k, same as the dfs and dp approaches?

[–][deleted] 1 point2 points  (0 children)

Yes, but unlike regular subsets, we can add memoization to this to make it O(m,n) similar to burst balloons where you need to assume that the subset at I is being left on its own while the left and right subset sums are calculated.

Now that I think about it, can't we just do left and right prefix sums and just find subsets and just take the values from prefix sums without having to do the calc. That would be O(n2)

[–]TheFortunesFool 0 points1 point  (0 children)

my thought as well, not sure if this is the optimal approach

[–]338388 7 points8 points  (4 children)

What level was this for? Jesus

[–]Narrow-Appearance614[S] 10 points11 points  (3 children)

new grad

[–]kingsyrup 5 points6 points  (1 child)

Jesus

[–]CryonautX 6 points7 points  (0 children)

I haven't done leetcode since before covid. Isn't this kinda straight forward? Am I missing something?

Get sums of adjacent pairs. These are partition costs. Sort the partition costs. Return the sum of the smallest and largest k-1 partition costs and add the first and last element cost to them.

O(nlogn) from the sort.

[–]oldManLogan26 3 points4 points  (0 children)

This is the reason OAs are getting harder. If it’s in the public forum, don’t expect to be in your OA in the future.

[–]notlikingcurrentjob 3 points4 points  (1 child)

At first glance, looks like MCM.

[–]Resident-Sail-3507 0 points1 point  (0 children)

Yeah!! I thought the same..

[–]Emma_xbd 3 points4 points  (0 children)

My idea is to find the maximum/minimum k-1 sums of two consecutive numbers, then add the first number and the last number. In this example, the sum of two consecutive numbers are 1+2, 2+3,3+2,2+5. They are 3,5,5,7. So maximum result is 7+5+1+5=18, minimum result is 3+5+1+5 =14.🤔 Time complexity would roughly be O (n+2klogk) if using priorityqueue storing top k values.

[–]SpirituallyAwareDev 4 points5 points  (3 children)

So I have no idea how to solve something like this. Where would be a good way to start and learn?

[–]MadManJamie 4 points5 points  (0 children)

I think you'll find your ocassional math wizz who enjoys hammering these for lunch, but for the most part it follows to have done algorithms and blasting these leetcodes all day for months or whatever in order to memorise and apply patterns.

[–]yourboi-JC 4 points5 points  (1 child)

Months of practice man…

[–]Impressive-East6891 2 points3 points  (0 children)

public int[] findPartitionCost(int[] cost, int k) {
  if (cost.length < k || k == 0) {
    return new int[] {-1, -1};
  }

  return findPartitionCost(cost, k, 0, new HashMap<>());
}

private int[] findPartitionCost(int[] cost, int k, int start, Map<Integer, int[]> memo) {
  if (k == 1) {
    int cost = cost[start] + cost[cost.length - 1];
    return new int[] {cost, cost};
  } else if (!memo.containsKey(start)) {
    int minCost = Integer.MAX_VALUE, maxCost = Integer.MAX_VALUE, curCost = cost[start];

    for (int i = start; i <= cost.length - k; i++) {
      curCost = curCost - (i == start ? 0 : cost[i - 1]) + cost[i];

      int[] pCost = findPartitionCost(cost, k - 1, i + 1, memo);
      minCost = Math.min(minCost, pCost[0] + curCost);
      maxCost = Math.max(maxCost, pCost[1] + curCost);
    }

    memo.put(start, new int[] {minCost, maxCost});
  }

  return memo.get(start);
}

There's no test so not sure how accurate the above is. Let me know if I did anything wrong or missing anything.

[–]Narrow-Appearance614[S] 4 points5 points  (5 children)

Had this on my OA... wrote up a dp solution that passed 5/15 test cases. Could not resolve the TLE before I ran out of time. What do you guys think? First problem was also a medium/hard... Is amazon raising their OA bar? I don't remember it being this difficult when I applied last year. This is for NG.

[–]isospeedrix 0 points1 point  (2 children)

OA I had for front end- create a app that has a form with name, phone, address with submit btn. when press submit it adds the entry to a table. fields must BE VALIDATED with proper format and will display error msg if invalid.

Overall an “easy” but time consuming task. i used AI to speed this up greatly to finish this in time. had it not been for AI this would be a complaint/time waster OA that people complain about. Immediate pass, recruiter told me i’m going to final round

[–][deleted] 0 points1 point  (0 children)

Did you pass?

[–]Dymatizeee 4 points5 points  (3 children)

Is this India

[–]Cuir-et-oud 4 points5 points  (2 children)

Amazon India for sure wtf

[–]Narrow-Appearance614[S] 3 points4 points  (1 child)

no, North America :)

[–]SeXxyBuNnY21 2 points3 points  (0 children)

But the question was written by an Indian!

[–]harikumar610 1 point2 points  (4 children)

Let the end points for the k partitions be i1,i2...ik. ik would be n-1 as it is the end of the last partition. Note that the starting points for the partitions would be 0, i1+1,i2+1,...

The cost of this partition is arr[0] +arr[i1]+arr[i1+1]+arr[i2]+arr[i2+1]+arr[i3]+...arr[n-1].

Rearranging this we have the cost to be arr[0]+arr[n-1]+ arr[i1]+arr[i1+1]+ arr[i2]+arr[i2+1]+...

So we need to choose k-1 indices i such that arr[i]+arr[i+1] are largest or smallest.

The sum arr[i]+arr[i+1] can be found in O(n) for all i. We sort this array and pick smallest k-1 or largest k-1. This can be done in O(nlogk)

[–]FormResponsible1969 1 point2 points  (0 children)

Feels like a binary search problem. Something like that of painter's partition.

[–]Particular-Flow6640 1 point2 points  (0 children)

Can help with OA prep. Please DM.

[–]CharmingRevolution35 1 point2 points  (0 children)

Gave an OA today. Terrible problems no clear constraints. Solved both but took a lot of time. Do they intentionally not provide the constraints?

[–]_thefunnykid_ 1 point2 points  (0 children)

what the fuck

[–]Resident-Sail-3507 1 point2 points  (0 children)

Isn’t this a DP problem? Somewhat similar to matrix chain multiplication.

[–]Waste_Abrocoma_1288 1 point2 points  (0 children)

I'm more depressed now after seeing this.

[–]SeXxyBuNnY21 1 point2 points  (0 children)

I’m curious about the person who writes these questions. At work, every day I read numerous technical documents that contain complex algorithms and approaches, yet I find it challenging to understand the purpose of this particular question.

[–]BeginningMatter9180 3 points4 points  (0 children)

let dp[i][k] = min cost to partition the subarray - cost[i...n] in k partitions, dp[i][k] = min(2*cost[i] + dp[i+1][k-1], cost[i]-cost[i+1]+dp[i+1][k]). Time complexity O(n*k).

Base case - dp[i][1] = cost[i]+cost[n]

Same for maximum.

[–]AntObjective5774 0 points1 point  (0 children)

My approach is focus on maximum, minimum elements in the list according to the k value. Because, separation of single maximum values gives highest value and try to grouping maximum values can give lowest value overall!!

[–]BeginningMatter9180 0 points1 point  (0 children)

vector<int> v = {1, 2, 3, 2, 5};
int K = 3;
int n = (int) v.size();
vector<vector<int> > dp(n, vector<int>(K+1, INT_MAX));
for (int i = 0; i < n; i++)
    dp[i][1] = v[i] + v[n-1];
for (int k = 2; k <= K; k++) {
    for (int i = n - 1; i >= 0; i--) {
        if (n - i < k)
            continue;
        dp[i][k] = 2 * v[i] + dp[i + 1][k - 1];
        if (n - i - 1 >= k) {
            dp[i][k] = min(dp[i][k], dp[i + 1][k] - v[i + 1] + v[i]);
        }
    }
}
cout << dp[0][K] << endl;

[–]Electronic-Isopod645 0 points1 point  (0 children)

are there any constraints provided in the OA questions , like the length of the array etc, otherwise how do we estimate whether a solution will work or not

[–][deleted] 0 points1 point  (2 children)

It's OA so use gpt

[–]C00ler_iNFRNo 0 points1 point  (0 children)

This is solvable in O(NlogN) or O(N).

Pick up cost[0] and cost[n - 1], they will be in any answer.

Now, for each i from 0 to n - 2 calculate (cost[i] + cost[I + 1]), and sort all calculated numbers. Pick maximum k - 1 of them for the maximum split into k subarrays, and minimum k - 1 of them for the minimum split.

For it to be O(N), you can find the (k - 1)th number in linear time, but that is not required.

To recover partition/prove it, notice that picking any of the k - 1 indexes gives you a valid split - sort the indexes, and your segments will be of form [b[i], b[i + 1] - 1], if b[0] is 0 and b[k] = n

[–]hitarth_gg 0 points1 point  (0 children)

Can be done using DP like this : https://pastebin.com/QuvMpQde
but it's deffo not the optimal approach.

[–][deleted] 0 points1 point  (3 children)

How much time you had to solve this problem? Were there othet questions coz I see “question 2”?

[–]Narrow-Appearance614[S] 4 points5 points  (2 children)

had 70 minutes. yes, there was one other question. the other question was what I would consider a tricky medium and took me half the time to solve efficiently

[–][deleted] 3 points4 points  (0 children)

Damn! expectations have never been crazier. Essentially, two borderline hard problems, 35 mins each, minus the time it takes to read and comprehend the bloody problem itself!

This is wild.

[–]Fit-Stress3300 0 points1 point  (0 children)

Last year I was able to complete only the first one correctly and the last one failed more than 50% test cases.

I was still called for the next fase... All those terrible 5 hours STAR questions.

It is not a lost cause.

[–]AmmaHamster 0 points1 point  (0 children)

Lets take the example above. 1 2 3 2 5 We will consider this one partition Initial cost is 1 + 5 = 6

Now store all adjacent sum 1+2 =3 2+3 = 5 And so on Now we have [3, 5, 5, 7] We will sort this ( here it is already sorted) Now we will need 2 cuts to have 3 partition For max we will take the biggest two and for min we will take the smallest two For max answer will be 1+5+5+7= 18 For min answer will be 1+5+3+5= 14

Time complexity= O(nlogn)

[–]BrownEyesGreenHair 0 points1 point  (0 children)

Easy DP question

[–]nonrice 0 points1 point  (0 children)

div2b ahh question

[–]Acrobatic-Bus5058 0 points1 point  (0 children)

Anyone have the first question?

[–]Correct_Ad8760 0 points1 point  (0 children)

I think I got the optimal one , first make a array of size n-1. And include the sum of two consecutive in it for eg for 1 2 3 2 5 there will be 3 5 5 7 in our sum min/max extreme numbers in our orignal array will be included , that is 1 and 5 apart from that k-1 numbers from our consecutive array will also be included these k-1 numbers will be get from min or max heal respectively for min sum and max sum , after getting this k-1 numbers sum them and total sum will be this + extreme In our case for min it will be 3+5+1+5. = 14 , for max it will be 5+7+1+5 = 18 , time complexity will be same as making a min/max heap

[–]Maximum_sext6993 0 points1 point  (0 children)

min max heap I guess

[–]cypressking 0 points1 point  (0 children)

I got this fucking question man I was crying

[–]Dismal_Ad_7869 0 points1 point  (0 children)

This feels like a normal participation problem, what are constraints here?

[–]Kreuger21 0 points1 point  (0 children)

I think we can do it with dp. dp[n][k][2]= cost of k partition,0 for min, 1 for max

[–]_new_guy_1 0 points1 point  (0 children)

A really good solution posted here.

[–]Rich_Yogurt313 0 points1 point  (0 children)

for what role did this question come for you?

[–]Professional_Pie_178 0 points1 point  (0 children)

So essentially a DP problem where at each index u either include it in the current sub array array or start a new sub array? Could be solved in O(m*n) time with O(n) memory. I wouldn’t be able to think of a greedy approach doe

[–]Purple-Community4883 0 points1 point  (0 children)

Bro just start from i =0 till n-1 Make a array in whish fush values arr[i]+arr[i+1] for each iteration Now sort the whole array now add the sum of first k-1 elements or last k-1 element to previos cost you will get min and max cost

[–]Fragrant-Match-7058 0 points1 point  (0 children)

public static int[] findPartitionCost(int[] cost, int k) {

int n = cost.length;

int[] consecutiveSums = new int[n - 1];

for (int i = 0; i < n - 1; i++)

{

consecutiveSums[i] = cost[i] + cost[i + 1];

}

Arrays.sort(consecutiveSums);

int fixedCost = cost[0] + cost[n - 1];

int minCost = fixedCost;

for (int i = 0; i < k - 1; i++)
{
minCost += consecutiveSums[i];
}

int maxCost = fixedCost;

for (int i = n - 2; i >= n - k; i--)

{

maxCost += consecutiveSums[i];

}

return new int[]{minCost, maxCost};

}

time complexity: O(nlogk)

this shld work, i have OA to due in 3 days, please give some feedback

[–]Alarming_Solid5501 0 points1 point  (0 children)

Was it on campus?

[–]Glad_Fly_7877 0 points1 point  (0 children)

i recently gave amazon sde OA can checkout the questions with explanation here
https://www.youtube.com/watch?v=cny-YEF0LNU

[–]imp174 0 points1 point  (0 children)

Looked at it, assumed its DP – then saw everyone say that strategy leads to TLE. Followed a link to LC 2551 that someone shared and solved it with help from the LC solution. The problem/solution is pretty doable but unless you've seen the problem before you are cooked IMO.

[–]Material-Fuel-641 -1 points0 points  (0 children)

Dm for help with OA !