Skip to content

Commit 83ba510

Browse files
committed
.
1 parent 3d04853 commit 83ba510

File tree

3 files changed

+163
-24
lines changed

3 files changed

+163
-24
lines changed

2024 January/Daily 01-01-24.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Leetcode Daily Challenge Solutions
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.
4+
5+
6+
## A very Happy Near Year to all you guys, may god give you strength to overcome what you want.
7+
## Always here to assist you guys.
8+
9+
## Todays 01-01-24 [Problem Link](https://leetcode.com/problems/assign-cookies/description/?envType=daily-question&envId=2024-01-01)
10+
11+
# Intuition
12+
<!-- Describe your first thoughts on how to solve this problem. -->
13+
Unitary method.
14+
15+
# Approach
16+
<!-- 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'
22+
---
23+
Have a look at the code , still have any confusion then please let me know in the comments
24+
Keep Solving.:)
25+
26+
# Complexity
27+
- Time complexity : $$O(gs)$$
28+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
29+
30+
- Space complexity : $$O(1)$$
31+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
32+
33+
# Code
34+
```
35+
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+
}
48+
}
49+
}
50+
return c;
51+
}
52+
}
53+
```

2024 January/Daily 02-01-24.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 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/)
2+
3+
# Intuition
4+
<!-- Describe your first thoughts on how to solve this problem. -->
5+
Unitary way to think.
6+
7+
# Approach
8+
<!-- Describe your approach to solving the problem. -->
9+
- I counted the frequencies of every number in the given array
10+
- - used HashMap for that
11+
- The maximum frequency of all element will be the number of answer rows
12+
- - as in every row there should be unique elements
13+
- Created and initialised answerlist with empty sub-lists
14+
- Now, according to their frequencies adding elements to their rows
15+
- - elements with frequency '1' will be present in only first row
16+
- - elements with frequency '2' will be present in first and second row
17+
- - ... and so on
18+
---
19+
Have a look at the code , still have any confusion then please let me know in the comments
20+
Keep Solving.:)
21+
22+
23+
# Complexity
24+
- Time complexity : $$O(cm)$$
25+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
26+
27+
- Space complexity : $$O(c)$$
28+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
29+
$$c$$ : number of unique elements in array
30+
$$m$$ : maximum frequency of any element
31+
32+
# Code
33+
```
34+
class Solution {
35+
public List<List<Integer>> findMatrix(int[] nums) {
36+
HashMap<Integer, Integer> m = new HashMap<>();
37+
int mf = 0; // to store maximum frequency of any integer : it will determine the number of rows
38+
39+
// adding numbers in hashmap along with its frequencies
40+
for( int i = 0; i < nums.length; i++){
41+
m.putIfAbsent(nums[i], 0);
42+
m.put( nums[i], m.get(nums[i]) + 1 );
43+
mf = Math.max(mf, m.get(nums[i]));
44+
}
45+
List<List<Integer>> l = new ArrayList<>(); // to store the answer list
46+
// the element with maximum frequency will be present in every sub-list of answerlist
47+
// making 'mf' rows in answer list
48+
for( int i = 0; i < mf; i++){
49+
List<Integer> t = new ArrayList<>();
50+
l.add(t);
51+
}
52+
53+
// according to their frequencies adding elements to their rows
54+
// elements with frequency '1' will be present in only first row
55+
// elements with frequency '2' will be present in first and second row
56+
// ... and so on
57+
58+
for( int c : m.keySet() ){
59+
for( int f = 0; f < m.get(c); f++){
60+
l.get(f).add(c);
61+
}
62+
}
63+
return l;
64+
}
65+
}
66+
```

README.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,71 @@
33
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.
44

55

6-
## A very Happy Near Year to all you guys, may god give you strength to overcome what you want.
76
## Always here to assist you guys.
87

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

1110
# Intuition
1211
<!-- Describe your first thoughts on how to solve this problem. -->
13-
Unitary method.
12+
Unitary way to think.
1413

1514
# Approach
1615
<!-- 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
2225
---
2326
Have a look at the code , still have any confusion then please let me know in the comments
2427
Keep Solving.:)
25-
28+
29+
2630
# Complexity
27-
- Time complexity : $$O(gs)$$
31+
- Time complexity : $$O(cm)$$
2832
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
2933

30-
- Space complexity : $$O(1)$$
34+
- Space complexity : $$O(c)$$
3135
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
36+
$$c$$ : number of unique elements in array
37+
$$m$$ : maximum frequency of any element
3238

3339
# Code
3440
```
3541
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);
4868
}
4969
}
50-
return c;
70+
return l;
5171
}
5272
}
5373
```

0 commit comments

Comments
 (0)