Skip to content

Commit 286b2ab

Browse files
committed
.
1 parent db62f4d commit 286b2ab

25 files changed

+1386
-0
lines changed

2023 December/Daily 16-12-23.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
To be anagram both strings should have same characters with same frequencies.
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
8+
- Created a HashMap
9+
- - stored the characters of 's' along with its respective frequencies
10+
- Now checked every character of 't'
11+
- - if found in hashmap
12+
- - - decrease the frequency of that character by 1
13+
- - else ( it means not in hashmap)
14+
- - - return false ( as different character present )
15+
- If 't' is anagram then all frequencies of character should be zero
16+
- - to check this ran a 'for' loop
17+
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+
# Complexity
23+
- Time complexity : $$O(n)$$
24+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
25+
26+
- Space complexity : $$O(n)$$
27+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
28+
29+
# Code
30+
```
31+
class Solution {
32+
public boolean isAnagram(String s, String t) {
33+
if( s.length() != t.length()){
34+
return false;
35+
}
36+
HashMap<Character, Integer> h = new HashMap<>();
37+
// filling hashmap
38+
for( int i = 0; i < s.length(); i++){
39+
if( h.containsKey(s.charAt(i))){
40+
h.put(s.charAt(i) , h.get(s.charAt(i)) + 1);
41+
}
42+
else{
43+
h.put(s.charAt(i) , 1);
44+
}
45+
}
46+
/*for ( char name: h.keySet()) {
47+
int value = h.get(name);
48+
System.out.println( name + " " + value);
49+
}*/
50+
51+
// checking 't'
52+
for( int i = 0; i < t.length(); i++){
53+
if( h.containsKey(t.charAt(i))){
54+
h.put(t.charAt(i) , h.get(t.charAt(i)) - 1);
55+
}
56+
else{
57+
return false;
58+
}
59+
}
60+
/*for ( char name: h.keySet()) {
61+
int value = h.get(name);
62+
System.out.println( name + " " + value);
63+
}*/
64+
65+
// ensuring all frequencies are zero
66+
for( char c : h.keySet()){
67+
if( h.get(c) != 0){
68+
return false;
69+
}
70+
}
71+
return true;
72+
}
73+
}
74+
```

2023 December/Daily 20-12-23.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Sum up the first two chocolates
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
- Sorted the array
8+
- The first two chocolates will have the least possible expenses
9+
- Checked the sum of both prices with 'money'
10+
- - if less than 'money' then simply return the leftover
11+
- - else return the whole 'money' ( as asked )
12+
---
13+
Have a look at the code , still have any confusion then please let me know in the comments
14+
Keep Solving.:)
15+
16+
# Complexity
17+
- Time complexity : $$O(logn)$$
18+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
19+
20+
- Space complexity : $$O(1)$$
21+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
22+
23+
# Code
24+
```
25+
class Solution {
26+
public int buyChoco(int[] prices, int money) {
27+
Arrays.sort(prices);
28+
// Checking the sum of both prices with 'money'
29+
// if less than 'money' then simply return the leftover
30+
//else return the whole 'money' ( as asked )
31+
if( prices[0] + prices[1] <= money){
32+
return money - ( prices[0] + prices[1] ) ;
33+
}
34+
else{
35+
return money;
36+
}
37+
38+
}
39+
}
40+
```

2023 December/Daily 22-12-23.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Little visualisation
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
- I counted the number of '1'
8+
- Initialised two counters
9+
- - 'zl' to store number of'0' on left
10+
- - 'or' to store number of'1' on right
11+
- Traversed from left to right
12+
- - if '0' encountered then increment the zero count
13+
- - else decrement the '1' count as it will no longer belong to right substring
14+
- - update score if required
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+
$$l$$ : length of string
22+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
23+
24+
- Space complexity : $$O(1)$$
25+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
26+
27+
# Code
28+
```
29+
class Solution {
30+
public int maxScore(String s) {
31+
int ones = 0;
32+
33+
// Counting the '1'
34+
for( int i = 0; i < s.length(); i++){
35+
if( s.charAt(i) == '1'){
36+
ones++;
37+
}
38+
}
39+
int zl = 0; // to store zeroes on left
40+
int or = ones; // to store ones in right
41+
int score = Integer.MIN_VALUE; // to store the 'score'
42+
43+
for( int i = 0; i < s.length()-1; i++){
44+
// if '0' encountered then increment the zero count
45+
if( s.charAt(i) == '0'){
46+
zl++;
47+
}
48+
// else decrement the '1' count as it will no longer belong to right substring
49+
else{
50+
or--;
51+
}
52+
score = Math.max(score, zl + or );
53+
}
54+
return score;
55+
}
56+
}
57+
```

2023 December/Daily 23-12-23.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Applying unitary approach
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
- I kept track of all coordinates visited along the movement
8+
- - used HashMap for that
9+
- If any coordinates found to be repeated then simply returned 'true'
10+
- Otherwise 'false'
11+
---
12+
Have a look at the code , still have any confusion then please let me know in the comments
13+
Keep Solving.:)
14+
15+
# Complexity
16+
- Time complexity : $$O(l)$$
17+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
18+
19+
- Space complexity : $$O(l^2)$$
20+
As I'm storing coordinates : so in worst case it may possible that it takes that much space
21+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
22+
23+
# Code
24+
```
25+
class Solution {
26+
public boolean isPathCrossing(String path) {
27+
HashMap<Integer, HashSet<Integer> > coord = new HashMap<>();
28+
int x = 0; // to keep track of x-coordinate of present location
29+
int y = 0; // to keep track of y-coordinate of present location
30+
HashSet<Integer> h0 = new HashSet<Integer>();
31+
h0.add(0);
32+
coord.put( 0, h0 );
33+
for( int i = 0; i < path.length(); i++){
34+
if( path.charAt(i) == 'N'){
35+
y++;
36+
}
37+
else if( path.charAt(i) == 'S'){
38+
y--;
39+
}
40+
else if( path.charAt(i) == 'E'){
41+
x++;
42+
}
43+
else if( path.charAt(i) == 'W'){
44+
x--;
45+
}
46+
//System.out.println(x + " " + y);
47+
// if new x-coordinate found then add to hashmap
48+
if( ! coord.containsKey(x)){
49+
coord.put(x, new HashSet<Integer>() );
50+
}
51+
// if coordinates found to be repeated then simply return 'true'
52+
if( coord.get(x).contains(y) ){
53+
return true;
54+
}
55+
// add the new coordinates as it isn't repeated
56+
coord.get(x).add(y);
57+
}
58+
return false;
59+
}
60+
}
61+
```

2023 December/Daily 24-12-23.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Simply checked for both type of patterns ( 0101.. and 1010.. )
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
Simply checked for both type of patterns ( 0101.. and 1010.. )
8+
- Variable 'a10' to store the number of changes required to make string of pattern '1010..'
9+
- - Iterated whole string to find 'a10'
10+
- Variable 'a01' to store the number of changes required to make string of pattern '0101..'
11+
- Returned the minimum of 'a10' and 'a01'
12+
---
13+
Have a look at the code , still have any confusion then please let me know in the comments
14+
Keep Solving.:)
15+
16+
# Complexity
17+
- Time complexity : $$O(n)$$
18+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
19+
20+
- Space complexity : $$O(1)$$
21+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
22+
23+
# Code
24+
```
25+
class Solution {
26+
public int minOperations(String s) {
27+
int a10 = 0; // to store changes required to make string of type '1010..'
28+
for( int i = 0; i < s.length(); i++){
29+
if( s.charAt(i) - '0' == i % 2){
30+
a10++;
31+
}
32+
}
33+
int a01; // to store changes required to make string of type '0101..'
34+
a01 = s.length() - a10;
35+
return Math.min(a10, a01);
36+
}
37+
}
38+
```

2023 December/Daily 29-12-23.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Dynamic to rescue.
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
- Broken into simpler
7+
- gp[t][din] will store minimum difficulty to complete first 't' tasks in 'din' days
8+
---
9+
Have a look at the code , still have any confusion then please let me know in the comments
10+
Keep Solving.:)
11+
# Complexity
12+
- Time complexity : $$O(l^2d)$$
13+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
14+
15+
- Space complexity : $$O(ld)$$
16+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
17+
$$l$$ : length of jobDifficulty
18+
19+
# Code
20+
```
21+
class Solution {
22+
public int minDifficulty(int[] jobDifficulty, int d) {
23+
if( jobDifficulty.length < d){
24+
return -1;
25+
}
26+
int[][] gp = new int[jobDifficulty.length+1][d+1];
27+
// gp[t][din] will store minimum difficulty to complete first 't' tasks in 'din' days
28+
Arrays.stream(gp).forEach(r -> Arrays.fill(r, Integer.MAX_VALUE/3));
29+
gp[0][0] = 0;
30+
31+
for( int i = 1; i <= jobDifficulty.length; i++){
32+
for( int j = 1; j <= d; j++){
33+
int m = 0;
34+
for( int k = i - 1; k >= j - 1; k--){
35+
m = Math.max( m, jobDifficulty[k] );
36+
gp[i][j] = Math.min( gp[i][j], gp[k][j-1] + m);
37+
}
38+
}
39+
}
40+
return gp[jobDifficulty.length][d];
41+
}
42+
}
43+
```

2023 December/Daily 30-12-23.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Basic unitary approach.
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
- I stored characters with their number of occurences in a hashmap
8+
- Checked if a character can be divided equally into all the strings or not
9+
- - if no then return false ( as it cann't be divided equally to all strings present in the array)
10+
- else return true
11+
---
12+
Have a look at the code , still have any confusion then please let me know in the comments
13+
Keep Solving.:)
14+
15+
# Complexity
16+
- Time complexity : $$O(as)$$
17+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
18+
$$a$$ : length of array
19+
$$s$$ : maximum length of any string
20+
- Space complexity : $$O(c)$$
21+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
22+
$$O(c)$$ : number of distinct characters in the array
23+
# Code
24+
```
25+
class Solution {
26+
public boolean makeEqual(String[] words) {
27+
HashMap<Character, Integer> m = new HashMap<>();
28+
// storing characters with their number of occurences in the hashmap
29+
for( int i = 0; i< words.length; i++){
30+
for( int j = 0; j < words[i].length(); j++){
31+
m.putIfAbsent(words[i].charAt(j), 0);
32+
m.put( words[i].charAt(j), m.get(words[i].charAt(j)) + 1 );
33+
}
34+
}
35+
// checking if a character can be divided equally into all the strings or not
36+
for( char c : m.keySet()){
37+
if( m.get(c) % words.length != 0){
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)