|
3 | 3 | This is my attemp to make the coding experience easier for you guys so that you can easily learn what to do in today's leetcode challenge.
|
4 | 4 |
|
5 | 5 |
|
6 |
| -## A very Happy Near Year to all you guys, may god give you strength to overcome what you want. |
7 | 6 | ## Always here to assist you guys.
|
8 | 7 |
|
9 |
| -## Todays 01-01-24 [Problem Link](https://leetcode.com/problems/assign-cookies/description/?envType=daily-question&envId=2024-01-01) |
| 8 | +## Today's 02-01-24 [Problem Link](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/solutions/4490329/daily-02-01-24/) |
10 | 9 |
|
11 | 10 | # Intuition
|
12 | 11 | <!-- Describe your first thoughts on how to solve this problem. -->
|
13 |
| -Unitary method. |
| 12 | +Unitary way to think. |
14 | 13 |
|
15 | 14 | # Approach
|
16 | 15 | <!-- Describe your approach to solving the problem. -->
|
17 |
| -- I sorted both arrays - 'g' & 's' |
18 |
| -- Started from first element of 'g' |
19 |
| -- - checked from first element for element greater than 'g[i]' in 's[j]' |
20 |
| -- - if found then incremented the count and nullify that 's[j]' so that that cookie cann't be considered in next child |
21 |
| -- Ran this loop for all elements of 'g' |
| 16 | +- I counted the frequencies of every number in the given array |
| 17 | +- - used HashMap for that |
| 18 | +- The maximum frequency of all element will be the number of answer rows |
| 19 | +- - as in every row there should be unique elements |
| 20 | +- Created and initialised answerlist with empty sub-lists |
| 21 | +- Now, according to their frequencies adding elements to their rows |
| 22 | +- - elements with frequency '1' will be present in only first row |
| 23 | +- - elements with frequency '2' will be present in first and second row |
| 24 | +- - ... and so on |
22 | 25 | ---
|
23 | 26 | Have a look at the code , still have any confusion then please let me know in the comments
|
24 | 27 | Keep Solving.:)
|
25 |
| - |
| 28 | + |
| 29 | + |
26 | 30 | # Complexity
|
27 |
| -- Time complexity : $$O(gs)$$ |
| 31 | +- Time complexity : $$O(cm)$$ |
28 | 32 | <!-- Add your time complexity here, e.g. $$O(n)$$ -->
|
29 | 33 |
|
30 |
| -- Space complexity : $$O(1)$$ |
| 34 | +- Space complexity : $$O(c)$$ |
31 | 35 | <!-- Add your space complexity here, e.g. $$O(n)$$ -->
|
| 36 | +$$c$$ : number of unique elements in array |
| 37 | +$$m$$ : maximum frequency of any element |
32 | 38 |
|
33 | 39 | # Code
|
34 | 40 | ```
|
35 | 41 | class Solution {
|
36 |
| - public int findContentChildren(int[] g, int[] s) { |
37 |
| - // sorting both the arrays |
38 |
| - Arrays.sort(g); |
39 |
| - Arrays.sort(s); |
40 |
| - int c = 0; // to store the number of contended children |
41 |
| - for( int i = 0; i < g.length; i++){ |
42 |
| - for( int j = 0; j < s.length; j++){ |
43 |
| - if( s[j] >= g[i]){ |
44 |
| - c++; |
45 |
| - s[j] = 0; |
46 |
| - break; |
47 |
| - } |
| 42 | + public List<List<Integer>> findMatrix(int[] nums) { |
| 43 | + HashMap<Integer, Integer> m = new HashMap<>(); |
| 44 | + int mf = 0; // to store maximum frequency of any integer : it will determine the number of rows |
| 45 | + |
| 46 | + // adding numbers in hashmap along with its frequencies |
| 47 | + for( int i = 0; i < nums.length; i++){ |
| 48 | + m.putIfAbsent(nums[i], 0); |
| 49 | + m.put( nums[i], m.get(nums[i]) + 1 ); |
| 50 | + mf = Math.max(mf, m.get(nums[i])); |
| 51 | + } |
| 52 | + List<List<Integer>> l = new ArrayList<>(); // to store the answer list |
| 53 | + // the element with maximum frequency will be present in every sub-list of answerlist |
| 54 | + // making 'mf' rows in answer list |
| 55 | + for( int i = 0; i < mf; i++){ |
| 56 | + List<Integer> t = new ArrayList<>(); |
| 57 | + l.add(t); |
| 58 | + } |
| 59 | + |
| 60 | + // according to their frequencies adding elements to their rows |
| 61 | + // elements with frequency '1' will be present in only first row |
| 62 | + // elements with frequency '2' will be present in first and second row |
| 63 | + // ... and so on |
| 64 | +
|
| 65 | + for( int c : m.keySet() ){ |
| 66 | + for( int f = 0; f < m.get(c); f++){ |
| 67 | + l.get(f).add(c); |
48 | 68 | }
|
49 | 69 | }
|
50 |
| - return c; |
| 70 | + return l; |
51 | 71 | }
|
52 | 72 | }
|
53 | 73 | ```
|
0 commit comments