Блог пользователя akshit5638

Автор akshit5638, история, 21 месяц назад, По-английски

we have an array of size n.each element of array is between 1 and 10.we are also given an integer k.We can divide array in atmost k subsets.After dividing, each element should belong to exactly one subset.We have to tell if it is possible to divide such that every subset sum<=10

constraints:

test case,t<=100

n<=1000

1<=a[i]<=10

1<=k<=100

example

t=1

n=12

2 1 2 5 4 3 6 1 1 9 3 2

k=4

than a valid division is

[9,1],[6,1,3],[5,2,3],[4,2,2,1]

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Google ?

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

This showed up in my online assessment too, I feel the testCases for this problem were quite weak cause what I did was sort the array in descending order and keep a bags array B. now for each element A[i] i try to find some bag by iterating B which A[i] can fit... if for any A[i] it can't fit then it's false, else true. This solution is definitely wrong but it passed all the testCases somehow.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

This question is similar to the bin packing problem, but there is an upper bound on the number of bins to be used.

The original bin packing problem is NP-complete, but I am not sure about this problem. One of the possible ways to find the answer (yes or no) is that we could use multiple interations for solving this problem. I don't know if this would work but we can try and have a high chance of succeeding. Repeat this process multiple times:
1. Shuffle the original array randomly (You can try the descending array once).
2. Use the best-fit algorithm for bin packing to find a possible way to arrange.
3. if the best-fit algorithm finds number of bins to be less than or equal to k then answer is yes. else if best fit algorithm returns the number of bins for fitting to be "b". Then the optimal answer is greater than or equal to (b-1)/1.7 . If this value is greater than k then answer is no.

If you can't find a sure answer after all these iterations, then answer no.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I think i got a greedy approach but not sure if it may get wrong! We can sort the array in descending, for eg in your case ---> 9 6 5 4 3 3 2 2 2 1 1 1 Since we dont need to maintan the subset itself , instead we can maintain a vector of exactly k elements(max subsets possible) which represents sum left after filling each subset( initially set to 10) in the order mentioned below.

Order

The idea is to keep a pointer at i=0 then to then we move forward with pointer j (initially 0 representing the jth element of array) in the sorted array till sum at subset i<=10 also at each step decreasing sub[i](subset i -->sum) by v[j] , if at a step sub[i]-v[j]<0 we move pointer i to next subset, if(i exceeds k , we break the loop)

now for all left j till n-1 , we need to put the remaing elements left in any of subsets with sum remaining >=1. We can do this with maintaining a map of count sum left and and for each j to n-1 we check if there exist a lowerbound of v[j] and decrease the count. THE LAST PART OF THE PROCESS MAY BE INEFFICIENT AND I DIDN,T PUT MUCH THOUGHT IN THINKING OF OPTIMIZATION .

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If I'm not wrong, this problem is analogous to the Elevator Rides problem on CSES problem set. Assume the numbers in the array to be weights of people which have to be transported via a lift. The lift can only take a certain amount of load(which is 10 according to your problem). Therefore you need to find the minimum number of times you will need to use the lift in order to transport all the people. If the number is <= k(here), then its possible, otherwise not. You can look up to the DP solution of this which also incorporates bit-masking. Also take a look at this tutorial.

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

maybe i am wrong but still, i come up with this, i am just summing up to create 10 first than 9 than .... 1 so first i calculate the frequency of each element ex-

1 — 30

2 — 10

3 — 4

5 — 6

7 — 3

9 — 8

for 10 i will just add them alone now for other frequency their sum only matter till they contribute less than 10 for 1 i can consider sum as 1, 2, 3, ... 10 for 2 -> 2 , 4 , 6 , 8 , 10 so to do this i will need to take sum times 1 sum times 2 , sum times 3 .... and remove everything which sum up to 10 than do the same till sum up to 1

for this i could do using bitmask dp where i need to insure i am only taking 1 case from each 1 to 10 that is i cant take three 1 and 2 one at the same time