You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- Describe your first thoughts on how to solve this problem. -->
5
+
- Dynamic Programming
6
+
-- Algorithm must uses dynamic programming to efficiently update and maintain the longest increasing subsequence.
7
+
- Greedy Choice
8
+
-- The algorithm must makes a greedy choice to update the current longest increasing subsequence (a) based on the incoming elements from the input array (nums).
9
+
10
+
11
+
# Approach
12
+
<!-- Describe your approach to solving the problem. -->
13
+
- ArrayList a
14
+
-- I used 'a' to store the current longest increasing subsequence.
15
+
-- It starts as an empty ArrayList.
16
+
- Iterating through nums
17
+
-- My code iterates through each element 's' in the input array nums.
18
+
-- For each element :
19
+
--- If a is empty or s is greater than the last element in a, add s to a (greedy choice as it contributes to increasing subsequence).
20
+
--- Otherwise, found the correct position for s in a using the bs function, and update the element at that position with s.
21
+
- Binary Search Function 'bs':
22
+
-- The 'bs' function performs a binary search on the sorted ArrayList 'a' to find the index of 's' or the index where 's' should be inserted.
23
+
-- If 's' is found, it returns the index. If not found, it returns the negation of the insertion point. This insertion point is used to update the current increasing subsequence.
24
+
- Return Result :
25
+
-- Finally, I returned the length of the longest increasing subsequence is the size of the ArrayList a.
26
+
---
27
+
- If you're still having trouble understanding what the binary search functions does here, I would suggest you to please go through this site [search here for binarySearch](https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/util/Collections.html)
28
+
---
29
+
Have a look at the code , still have any confusion then please let me know in the comments
30
+
Keep Solving.:)
31
+
# Complexity
32
+
- Time complexity : $$O(l*log(l))$$
33
+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
34
+
35
+
- Space complexity : $$O(l)$$
36
+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
<!-- Describe your first thoughts on how to solve this problem. -->
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.
12
+
- Dynamic Programming
13
+
-- Algorithm must uses dynamic programming to efficiently update and maintain the longest increasing subsequence.
14
+
- Greedy Choice
15
+
-- The algorithm must makes a greedy choice to update the current longest increasing subsequence (a) based on the incoming elements from the input array (nums).
14
16
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.
17
17
18
18
# Approach
19
19
<!-- Describe your approach to solving the problem. -->
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
--- 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
20
+
- ArrayList a
21
+
-- I used 'a' to store the current longest increasing subsequence.
22
+
-- It starts as an empty ArrayList.
23
+
- Iterating through nums
24
+
-- My code iterates through each element 's' in the input array nums.
25
+
-- For each element :
26
+
--- If a is empty or s is greater than the last element in a, add s to a (greedy choice as it contributes to increasing subsequence).
27
+
--- Otherwise, found the correct position for s in a using the bs function, and update the element at that position with s.
28
+
- Binary Search Function 'bs':
29
+
-- The 'bs' function performs a binary search on the sorted ArrayList 'a' to find the index of 's' or the index where 's' should be inserted.
30
+
-- If 's' is found, it returns the index. If not found, it returns the negation of the insertion point. This insertion point is used to update the current increasing subsequence.
32
31
- Return Result :
33
-
-- Return the total number of operations needed to make all numbers in the array divisible by 3.
32
+
-- Finally, I returned the length of the longest increasing subsequence is the size of the ArrayList a.
33
+
---
34
+
- If you're still having trouble understanding what the binary search functions does here, I would suggest you to please go through this site [search here for binarySearch](https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/util/Collections.html)
34
35
---
35
36
Have a look at the code , still have any confusion then please let me know in the comments
36
37
Keep Solving.:)
37
-
38
38
# Complexity
39
-
- Time complexity : $$O(l)$$
39
+
- Time complexity : $$O(l*log(l))$$
40
40
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
41
41
42
-
- Space complexity : $$O(u)$$
43
-
44
-
$$l$$ : size of array
45
-
$$u$$ : number of unique letters in array
42
+
- Space complexity : $$O(l)$$
46
43
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
47
-
44
+
$$l$$ : length of array.
48
45
# Code
49
46
```
50
47
class Solution {
51
-
public int minOperations(int[] nums) {
52
-
boolean haveone = false;
53
-
HashMap<Integer, Integer> m = new HashMap<>();
54
-
for( int i = 0; i < nums.length; i++){
55
-
m.put( nums[i], m.getOrDefault(nums[i], 0) + 1);
56
-
}
57
-
int operations = 0;
58
-
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
0 commit comments