Skip to content

Commit 17fcd02

Browse files
committed
.
1 parent 83ba510 commit 17fcd02

File tree

3 files changed

+162
-41
lines changed

3 files changed

+162
-41
lines changed

2024 January/Daily 03-01-24.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Today's 03-01-24 [Problem Link](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/?envType=daily-question&envId=2024-01-03)
2+
3+
4+
# Intuition
5+
<!-- Describe your first thoughts on how to solve this problem. -->
6+
Basic multiplication.
7+
# Approach
8+
<!-- Describe your approach to solving the problem. -->
9+
- I kept track of number of '1' in a row
10+
- Now iterated over every row of array
11+
- - counted the number of '1' in current row
12+
- - number of beams will the product of current number of device and previous number of devices
13+
- - added the product to answer
14+
- - now, the current one will become the previous one to next row
15+
---
16+
Have a look at the code , still have any confusion then please let me know in the comments
17+
Keep Solving.:)
18+
19+
# Complexity
20+
- Time complexity : $$O(l)$$
21+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
22+
$$l$$ : length of array
23+
- Space complexity : $$O(1)$$
24+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
25+
26+
# Code
27+
```
28+
class Solution {
29+
public int numberOfBeams(String[] bank) {
30+
int jawab = 0; // to store answer
31+
int picheek = 0; // to store number of '1' in previous state
32+
33+
for( String r : bank){
34+
int ek = (int) r.chars().filter( g -> g == '1').count(); // counting the number of '1' in current row
35+
if( ek != 0){ // number of beams will the product of current number of device and previous number of devices
36+
jawab += picheek*ek; // adding the product to answer
37+
picheek = ek; // now, the current one will become the previous one to next row
38+
}
39+
}
40+
return jawab;
41+
}
42+
}
43+
```

2024 January/Daily 04-01-24.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## Today's 04-01-24 [Problem Link](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/description/)
2+
3+
# Intuition
4+
<!-- Describe your first thoughts on how to solve this problem. -->
5+
- Frequency Count :
6+
- - 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.
7+
8+
- Divisibility by 3 :
9+
- - 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.
10+
11+
# Approach
12+
<!-- Describe your approach to solving the problem. -->
13+
- HashMap for Frequency :
14+
- - Iterate through the input array (nums).
15+
Use a HashMap (m) to store the frequency of each number in the array.
16+
- Calculate Operations :
17+
- - Iterate through the keys of the HashMap (m).
18+
- - For each key 'k' observe it's occurence :
19+
- - - If the occurrence is 1, return -1 (as it's not possible to make it 0 by subtracting 2 or 3).
20+
- - - if occurence of number is divisible by 3, total operations = occurences/3
21+
- - - if occurence of number is of form 3*n + 1, let's try to break it down
22+
- - - - 3*n + 1 = 3*(n-1) + 4 = 3*(n-1) + 2*(2) -> n-1+2 operations = n+1
23+
- - - if occurence of number is of form 3*n + 2, let's try to break it down
24+
- - - - 3*n + 2 = 3*(n) + 2*(1) -> n+1 operations
25+
- Return Result :
26+
- - Return the total number of operations needed to make all numbers in the array divisible by 3.
27+
---
28+
Have a look at the code , still have any confusion then please let me know in the comments
29+
Keep Solving.:)
30+
31+
# Complexity
32+
- Time complexity : $$O(l)$$
33+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
34+
35+
- Space complexity : $$O(u)$$
36+
37+
$$l$$ : size of array
38+
$$u$$ : number of unique letters in array
39+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
40+
41+
# Code
42+
```
43+
class Solution {
44+
public int minOperations(int[] nums) {
45+
boolean haveone = false;
46+
HashMap<Integer, Integer> m = new HashMap<>();
47+
for( int i = 0; i < nums.length; i++){
48+
m.put( nums[i], m.getOrDefault(nums[i], 0) + 1);
49+
}
50+
int operations = 0;
51+
52+
// if occurence of number is divisible by 3, total operations = occurences/3
53+
// if occurence of number is of form 3*n + 1, let's try to break it down
54+
// 3*n + 1 = 3*(n-1) + 4 = 3*(n-1) + 2*(2) -> n-1+2 operations = n+1
55+
// if occurence of number is of form 3*n + 2, let's try to break it down
56+
// 3*n + 2 = 3*(n) + 2*(1) -> n+1 operations
57+
58+
for( int k : m.keySet()){
59+
if( m.get(k) == 1){
60+
return -1;
61+
}
62+
if( m.get(k) % 3 == 0){
63+
operations += m.get(k)/3;
64+
}
65+
else{
66+
operations += m.get(k)/3 + 1;
67+
}
68+
}
69+
return operations;
70+
}
71+
}
72+
```

README.md

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,79 @@
11
# Leetcode Daily Challenge Solutions
22

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

55

66
## Always here to assist you guys.
77

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/)
99

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

1418
# Approach
1519
<!-- 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.
2534
---
2635
Have a look at the code , still have any confusion then please let me know in the comments
2736
Keep Solving.:)
2837

29-
3038
# Complexity
31-
- Time complexity : $$O(cm)$$
39+
- Time complexity : $$O(l)$$
3240
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
3341

34-
- Space complexity : $$O(c)$$
42+
- Space complexity : $$O(u)$$
43+
44+
$$l$$ : size of array
45+
$$u$$ : number of unique letters in array
3546
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
36-
$$c$$ : number of unique elements in array
37-
$$m$$ : maximum frequency of any element
3847

3948
# Code
4049
```
4150
class Solution {
42-
public List<List<Integer>> findMatrix(int[] nums) {
51+
public int minOperations(int[] nums) {
52+
boolean haveone = false;
4353
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
4754
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);
5856
}
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;
6458
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;
6874
}
6975
}
70-
return l;
76+
return operations;
7177
}
7278
}
73-
```
79+
```

0 commit comments

Comments
 (0)