|
1 | 1 | # Leetcode Daily Challenge Solutions |
2 | 2 |
|
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. |
| 3 | +This is my attempt 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 | 6 | ## Always here to assist you guys. |
7 | 7 |
|
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/) |
| 8 | +## Today's 04-01-24 [Problem Link](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/description/) |
9 | 9 |
|
10 | 10 | # Intuition |
11 | 11 | <!-- Describe your first thoughts on how to solve this problem. --> |
12 | | -Unitary way to think. |
| 12 | +- Frequency Count : |
| 13 | +- - The code starts by creating a HashMap (m) to store the frequency of each number in the given array. This is crucial for determining how many times each number appears in the array. |
| 14 | + |
| 15 | +- Divisibility by 3 : |
| 16 | +- - The primary goal is to minimize the operations needed to make all elements 0. The code examines the frequency of each number and calculates the minimum operations required based on certain conditions. |
13 | 17 |
|
14 | 18 | # Approach |
15 | 19 | <!-- Describe your approach to solving the problem. --> |
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 |
| 20 | +- HashMap for Frequency : |
| 21 | +- - Iterate through the input array (nums). |
| 22 | +Use a HashMap (m) to store the frequency of each number in the array. |
| 23 | +- Calculate Operations : |
| 24 | +- - Iterate through the keys of the HashMap (m). |
| 25 | +- - For each key 'k' observe it's occurence : |
| 26 | +- - - If the occurrence is 1, return -1 (as it's not possible to make it 0 by subtracting 2 or 3). |
| 27 | +- - - if occurence of number is divisible by 3, total operations = occurences/3 |
| 28 | +- - - if occurence of number is of form 3*n + 1, let's try to break it down |
| 29 | +- - - - 3*n + 1 = 3*(n-1) + 4 = 3*(n-1) + 2*(2) -> n-1+2 operations = n+1 |
| 30 | +- - - if occurence of number is of form 3*n + 2, let's try to break it down |
| 31 | +- - - - 3*n + 2 = 3*(n) + 2*(1) -> n+1 operations |
| 32 | +- Return Result : |
| 33 | +- - Return the total number of operations needed to make all numbers in the array divisible by 3. |
25 | 34 | --- |
26 | 35 | Have a look at the code , still have any confusion then please let me know in the comments |
27 | 36 | Keep Solving.:) |
28 | 37 |
|
29 | | - |
30 | 38 | # Complexity |
31 | | -- Time complexity : $$O(cm)$$ |
| 39 | +- Time complexity : $$O(l)$$ |
32 | 40 | <!-- Add your time complexity here, e.g. $$O(n)$$ --> |
33 | 41 |
|
34 | | -- Space complexity : $$O(c)$$ |
| 42 | +- Space complexity : $$O(u)$$ |
| 43 | + |
| 44 | +$$l$$ : size of array |
| 45 | +$$u$$ : number of unique letters in array |
35 | 46 | <!-- Add your space complexity here, e.g. $$O(n)$$ --> |
36 | | -$$c$$ : number of unique elements in array |
37 | | -$$m$$ : maximum frequency of any element |
38 | 47 |
|
39 | 48 | # Code |
40 | 49 | ``` |
41 | 50 | class Solution { |
42 | | - public List<List<Integer>> findMatrix(int[] nums) { |
| 51 | + public int minOperations(int[] nums) { |
| 52 | + boolean haveone = false; |
43 | 53 | 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 | 54 | 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); |
| 55 | + m.put( nums[i], m.getOrDefault(nums[i], 0) + 1); |
58 | 56 | } |
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 |
| 57 | + int operations = 0; |
64 | 58 |
|
65 | | - for( int c : m.keySet() ){ |
66 | | - for( int f = 0; f < m.get(c); f++){ |
67 | | - l.get(f).add(c); |
| 59 | + // if occurence of number is divisible by 3, total operations = occurences/3 |
| 60 | + // if occurence of number is of form 3*n + 1, let's try to break it down |
| 61 | + // 3*n + 1 = 3*(n-1) + 4 = 3*(n-1) + 2*(2) -> n-1+2 operations = n+1 |
| 62 | + // if occurence of number is of form 3*n + 2, let's try to break it down |
| 63 | + // 3*n + 2 = 3*(n) + 2*(1) -> n+1 operations |
| 64 | + |
| 65 | + for( int k : m.keySet()){ |
| 66 | + if( m.get(k) == 1){ |
| 67 | + return -1; |
| 68 | + } |
| 69 | + if( m.get(k) % 3 == 0){ |
| 70 | + operations += m.get(k)/3; |
| 71 | + } |
| 72 | + else{ |
| 73 | + operations += m.get(k)/3 + 1; |
68 | 74 | } |
69 | 75 | } |
70 | | - return l; |
| 76 | + return operations; |
71 | 77 | } |
72 | 78 | } |
73 | | -``` |
| 79 | +``` |
0 commit comments