Skip to content

Commit 121f8d8

Browse files
committed
.
1 parent 3ec4414 commit 121f8d8

File tree

2 files changed

+102
-48
lines changed

2 files changed

+102
-48
lines changed

2024 January/Daily 05-01-24.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Today's 05-01-24 [Problem Link](https://leetcode.com/problems/longest-increasing-subsequence/description/?envType=daily-question&envId=2024-01-05)
2+
3+
# Intuition
4+
<!-- 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)$$ -->
37+
$$l$$ : length of array.
38+
# Code
39+
```
40+
class Solution {
41+
public int lengthOfLIS(int[] nums) {
42+
43+
ArrayList<Integer> a = new ArrayList<>();
44+
for( int s : nums){
45+
if( a.isEmpty() || s > a.get( a.size() - 1) ){
46+
a.add(s);
47+
}
48+
else{
49+
a.set( bs(a, s) , s);
50+
}
51+
}
52+
return a.size();
53+
}
54+
55+
static int bs( ArrayList<Integer> a, int s){
56+
int in = Collections.binarySearch(a, s);
57+
if( in < 0){
58+
return - in - 1;
59+
}
60+
return in;
61+
}
62+
}
63+
```

README.md

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,66 @@ This is my attempt to make the coding experience easier for you guys so that you
55

66
## Always here to assist you guys.
77

8-
## Today's 04-01-24 [Problem Link](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/description/)
8+
## Today's 05-01-24 [Problem Link](https://leetcode.com/problems/longest-increasing-subsequence/description/?envType=daily-question&envId=2024-01-05)
99

1010
# Intuition
1111
<!-- 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).
1416

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.
1717

1818
# Approach
1919
<!-- 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
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
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.
3231
- 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)
3435
---
3536
Have a look at the code , still have any confusion then please let me know in the comments
3637
Keep Solving.:)
37-
3838
# Complexity
39-
- Time complexity : $$O(l)$$
39+
- Time complexity : $$O(l*log(l))$$
4040
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
4141

42-
- Space complexity : $$O(u)$$
43-
44-
$$l$$ : size of array
45-
$$u$$ : number of unique letters in array
42+
- Space complexity : $$O(l)$$
4643
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
47-
44+
$$l$$ : length of array.
4845
# Code
4946
```
5047
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
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-
}
48+
public int lengthOfLIS(int[] nums) {
49+
50+
ArrayList<Integer> a = new ArrayList<>();
51+
for( int s : nums){
52+
if( a.isEmpty() || s > a.get( a.size() - 1) ){
53+
a.add(s);
54+
}
7255
else{
73-
operations += m.get(k)/3 + 1;
56+
a.set( bs(a, s) , s);
7457
}
7558
}
76-
return operations;
59+
return a.size();
60+
}
61+
62+
static int bs( ArrayList<Integer> a, int s){
63+
int in = Collections.binarySearch(a, s);
64+
if( in < 0){
65+
return - in - 1;
66+
}
67+
return in;
7768
}
7869
}
7970
```

0 commit comments

Comments
 (0)