Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 7 completed #552

Open
wants to merge 347 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
347 commits
Select commit Hold shift + click to select a range
8155e6f
z-function + algo
dirwolf Feb 5, 2024
761c07c
Reverse in array via indexes and ommitting count of largest element
dirwolf Feb 6, 2024
3aa3260
Create ALL ANAGRAMS PROBLEM
dirwolf Feb 6, 2024
b6a668c
Delete Day 31/ALL ANAGRAMS PROBLEM
dirwolf Feb 6, 2024
e5e3b87
Create 3 anagrams problems
dirwolf Feb 6, 2024
9975a66
Create Sort by freq LC
dirwolf Feb 7, 2024
3d9fc48
Create 279. Perfect squares(lagrange)
dirwolf Feb 8, 2024
3c5319b
Create 647. Palindromic Substrings
dirwolf Feb 10, 2024
809f25d
Count sort concept
dirwolf Feb 17, 2024
554fc53
O(nlogn) most optimized
dirwolf Feb 17, 2024
d54e1e0
O(nlogn) to o(n)
dirwolf Feb 17, 2024
2d82740
Delete Day 36 17feb. directory
dirwolf Feb 17, 2024
e99b8fc
Create 1481. Least Number of Unique Integers after K Removals 17feb
dirwolf Feb 17, 2024
e40f9b4
o(nlogn) to o(n)
dirwolf Feb 17, 2024
ac832ff
Create heap build from scratch and heapsort
dirwolf Feb 17, 2024
12b9f4c
Create 231. Power of Two
dirwolf Feb 20, 2024
895ef75
Create 268. Missing Number
dirwolf Feb 20, 2024
20fcfb4
Create CF 928 DIV 4
dirwolf Feb 21, 2024
1212811
Create 201. Bitwise AND of Numbers Range
dirwolf Feb 23, 2024
a5a0a30
Create 997. Find the Town Judge EASY
dirwolf Feb 23, 2024
3da8a5d
Fenwick tree and merge sort
dirwolf Feb 23, 2024
4024ac4
aLSO DP
dirwolf Feb 24, 2024
86958e0
Create Maximum INdex
dirwolf Feb 27, 2024
d6c6724
Create max and Second max
dirwolf Feb 27, 2024
85f36be
Create Check if arr is sorted and rotated
dirwolf Feb 27, 2024
77459e4
Create Reverse array in groups
dirwolf Feb 27, 2024
6a9c3d2
Create Kadane's algo
dirwolf Feb 27, 2024
9eae49c
Create Equilibrium point
dirwolf Feb 28, 2024
f4ee6b2
Create Rotate array
dirwolf Feb 28, 2024
3bd22f8
Create Sum of integer in alphanumeric string
dirwolf Feb 28, 2024
c69009d
Create Minimum adjacent differences in array
dirwolf Feb 28, 2024
0051b59
Create Reverse array in K groups
dirwolf Feb 28, 2024
ab7a33f
Create Even Odd Tree
dirwolf Feb 29, 2024
32bb849
Create 513. Find Bottom Left Tree Value
dirwolf Feb 29, 2024
1f2da1b
Create AVGPERM
dirwolf Mar 1, 2024
6e0f19a
Create GAME OF TWO STACKS (hackerrank)
dirwolf Mar 1, 2024
d83cc3d
Create Find peak element
dirwolf Mar 1, 2024
9217e2f
Create 2864 Mx odd binary
dirwolf Mar 1, 2024
dc38b24
Create Hotel Bytelandia
dirwolf Mar 2, 2024
7656066
Create Squares of A sorted array
dirwolf Mar 2, 2024
144fee2
Create Theft at World Bank
dirwolf Mar 2, 2024
72e6db4
Create The K weakest rows in a matrix
dirwolf Mar 2, 2024
1379ab8
Create PrefixSum2DQeury.cpp
dirwolf Mar 3, 2024
83bd2a8
Create 3070. Count Submatrices with Top-Left Element and Sum Less Than k
dirwolf Mar 3, 2024
3cead8c
Create Flipping bits
dirwolf Mar 3, 2024
7ea40b9
Create Maximize XOR
dirwolf Mar 3, 2024
61b1519
Create Find Indexes of a subarray with given sum
dirwolf Mar 4, 2024
c0eecf0
Create Max Sum Subarray of size K
dirwolf Mar 4, 2024
453df45
Create Find smallest +ve missing number
dirwolf Mar 4, 2024
8d9c31e
Create Maximum Circular array sum
dirwolf Mar 4, 2024
3b95d77
Create 1750. Minimum Length of String After Deleting Similar Ends
dirwolf Mar 6, 2024
268d9a8
O(1) space and O(N) time (-ve f)
dirwolf Mar 6, 2024
cf40d08
Make row and col vector and again traverse if either of em 1 make all…
dirwolf Mar 6, 2024
fef02e6
Fav problem, edge cases-more than one row/col ,startcol++,endcol-- ....
dirwolf Mar 6, 2024
e049f6a
Create Boundary traversal of matrix
dirwolf Mar 6, 2024
bcdb8b8
Create Print Matrix in snake Pattern
dirwolf Mar 6, 2024
a575210
Create Multiply the matrices
dirwolf Mar 6, 2024
63ad74c
// emphasize on this : i<=j
dirwolf Mar 6, 2024
5b9a290
n*(maximum of rowsum,colsum)-totalsum
dirwolf Mar 6, 2024
5d9b35f
Easy ,swaps first row with last
dirwolf Mar 6, 2024
ea349d0
CAN ALSO BE DONE VIA PQ
dirwolf Mar 6, 2024
5637591
Create Linked list cycle
dirwolf Mar 8, 2024
e0bef4c
Create Middle of Linked List
dirwolf Mar 8, 2024
bae3874
Use mp
dirwolf Mar 8, 2024
d15ad0b
USE mp
dirwolf Mar 8, 2024
74f4844
Solved on my own
dirwolf Mar 8, 2024
c36bd0a
restart j from i
dirwolf Mar 8, 2024
3e317c4
MAPS
dirwolf Mar 8, 2024
b339116
Create 904. Fruit Into Baskets
dirwolf Mar 9, 2024
ea054e5
Reserve and move(x)
dirwolf Mar 9, 2024
ef88b68
Use set instead of sliding and using map
dirwolf Mar 9, 2024
2e2b938
Check for Children Sum Property in a BT exist or not
dirwolf Mar 9, 2024
19d56dd
3 Subsequences pattern
dirwolf Mar 11, 2024
64c51f1
Create 236. Lowest Common Ancestor of a Binary Tree
dirwolf Mar 11, 2024
8ee459b
Create 349. Intersection of Two Arrays
dirwolf Mar 11, 2024
c453674
even appearing cancel outs, golden rule
dirwolf Mar 11, 2024
6a6cd86
Create and with kth set bit number
dirwolf Mar 12, 2024
832ca08
Create Custom Sort string
dirwolf Mar 12, 2024
6033b7d
m=(1<<r)-(1<<(l-1)); then &
dirwolf Mar 12, 2024
f8070f1
Put embedded value at top and while removing update minelement
dirwolf Mar 13, 2024
c529825
Create Parenthesis Checker
dirwolf Mar 13, 2024
7c1dc6f
stack will store indices
dirwolf Mar 13, 2024
21924f6
map<int,node*>
dirwolf Mar 13, 2024
ba845d1
remaining bits from pattern also
dirwolf Mar 13, 2024
cb9a352
Update Count set bits from 1 to N
dirwolf Mar 13, 2024
4e72c38
prefix_min,suffix_max
dirwolf Mar 13, 2024
f38fde2
Remember to use map comparison properly
dirwolf Mar 13, 2024
0f17483
Use sliding window properly
dirwolf Mar 13, 2024
e2d774c
1 to N , pivot is the sum's sqrt
dirwolf Mar 13, 2024
903004d
Its easy dont apply sliding mind
dirwolf Mar 13, 2024
b23fd2a
Create 930. Binary Subarrays With Sum
dirwolf Mar 14, 2024
7de9326
Sliding to remove only odds each time from left
dirwolf Mar 15, 2024
0e23ff0
pf[i] = pf[i-1]*arr[i-1] , same for suff,
dirwolf Mar 15, 2024
0f33bfa
Double pointers
dirwolf Mar 16, 2024
4d0f3ca
Applying merge sort in double pointers
dirwolf Mar 16, 2024
1a3828c
shrink by dividing ,ans += r-l+1
dirwolf Mar 16, 2024
f0f74ad
TO-DO
dirwolf Mar 16, 2024
6048ed5
use int pfSum not array,1:+1,0:-1
dirwolf Mar 16, 2024
396cfaf
57 . Merge interval TO-DO
dirwolf Mar 17, 2024
7bba1e7
dont use else statement
dirwolf Mar 17, 2024
74304de
Create Level order traversal
dirwolf Mar 17, 2024
067950d
make hash of head1 data and check x-head2
dirwolf Mar 17, 2024
65e6a1b
movement arrays dx dy
dirwolf Mar 18, 2024
e1c7a51
Sort via end point
dirwolf Mar 18, 2024
1850f4a
Create 169. Majority Element
dirwolf Mar 19, 2024
b13da7a
idle =idle - min(freq[0]-1,freq[i]);
dirwolf Mar 19, 2024
b740028
boundary conditions dont forget
dirwolf Mar 20, 2024
0164360
Karan edges
dirwolf Mar 21, 2024
1e6e6d4
Done by both bool vector and int vector
dirwolf Mar 21, 2024
39d1717
Sort b4 n after ,while(index+1<n ..
dirwolf Mar 21, 2024
da494ce
max diff b/w pairs,(diff+1)/2
dirwolf Mar 22, 2024
e3d0a56
To be done via dp
dirwolf Mar 22, 2024
5c65d4d
take first and check with rest ,vice versa,karan
dirwolf Mar 22, 2024
dc8a86d
updated method
dirwolf Mar 24, 2024
b454d9f
Create 3091. Apply Operations to Make Sum of Array Greater Than or Eq…
dirwolf Mar 25, 2024
a7e813b
Rename Find smallest +ve missing number to bavya Find smallest +ve mi…
dirwolf Mar 25, 2024
43d8f32
Create Rearrange elements in O(1)
dirwolf Mar 25, 2024
20b3cf4
O
dirwolf Mar 25, 2024
b2bde65
Bhavya
dirwolf Mar 26, 2024
ab4fcab
Simple recursion
dirwolf Mar 26, 2024
34e489f
O(1) mtl
dirwolf Mar 26, 2024
b3d415c
similar to 442. o(1) space
dirwolf Mar 26, 2024
fa52b6c
DP 1
dirwolf Mar 28, 2024
9b05d4a
Create if newly inserted element exceed frequencey poped it out from …
dirwolf Mar 28, 2024
75f87f1
mEMO
dirwolf Mar 29, 2024
918e011
RANGE based
dirwolf Mar 29, 2024
71dd7f1
Create 2958. Length of Longest Subarray With at Most K Frequency
dirwolf Mar 29, 2024
ce4f12b
ans+=(n-j) for a reason
dirwolf Mar 29, 2024
c10bb2e
using loops for k jumps
dirwolf Mar 29, 2024
da6b77c
ans++(n-j) for a areason
dirwolf Mar 29, 2024
cb67019
ans+=(n-j) for a reason
dirwolf Mar 29, 2024
5db2c7f
Create Combination sum (sort)
dirwolf Mar 30, 2024
f147008
Create 992. Subarrays with K Different Integers
dirwolf Mar 30, 2024
52f1769
Create 560. Subarray Sum Equals K
dirwolf Mar 30, 2024
822fa1b
Create 992. Subarrays with K Different Integers (atmost~binarry)
dirwolf Mar 31, 2024
c2473d5
Create 2444. Count Subarrays With Fixed Bounds
dirwolf Mar 31, 2024
bf2a799
Create GFG148 Maximize arr (B) hit
dirwolf Apr 1, 2024
6cf020b
Create 58. Length of Last Word (easy)
dirwolf Apr 1, 2024
aebc28f
Create GFG148 a (MAPS)
dirwolf Apr 1, 2024
ff71377
Create LC 391 (c -alternating subarrays)
dirwolf Apr 1, 2024
ab8c9dd
Create Aggressive Cows (vvip)
dirwolf Apr 1, 2024
130781a
Create Largest element K in arr[] such that K is smaller than or equa…
dirwolf Apr 1, 2024
525ffa2
Create Peak Element *
dirwolf Apr 2, 2024
4d17abf
Create Minimum Number in a sorted rotated array
dirwolf Apr 2, 2024
e97eca4
Update Custom Sort string
dirwolf Apr 2, 2024
22234b8
Create Isomorphic strings *
dirwolf Apr 2, 2024
b6a7a87
Create Day 69
dirwolf Apr 2, 2024
4a67566
Delete Day 69
dirwolf Apr 3, 2024
de77c13
Create 25. Reverse Nodes in k-Group
dirwolf Apr 3, 2024
576efcd
Create Activity selection km
dirwolf Apr 4, 2024
773f965
Create Largest number with given sum(if (sum > 9 * n)
dirwolf Apr 4, 2024
20f3d32
Create 200. Number of islands (on)
dirwolf Apr 4, 2024
701974e
Rename 560. Subarray Sum Equals K to 560. Subarray Sum Equals K(goal)
dirwolf Apr 4, 2024
7e46a7d
Update and rename 2444. Count Subarrays With Fixed Bounds to 2444. (h…
dirwolf Apr 4, 2024
b34a4c6
Update 560. Subarray Sum Equals K(goal)
dirwolf Apr 4, 2024
c71755e
Create *Longest subarray with sum k
dirwolf Apr 5, 2024
27e2069
Create Job Sequencing (greedy)
dirwolf Apr 5, 2024
ef0f964
Create Longest Common subsequence
dirwolf Apr 5, 2024
87286f3
Update and rename ay 64/2958. Length of Longest Subarray With at Most…
dirwolf Apr 5, 2024
82c2c9b
Create (2 methods)Count alternating subarray
dirwolf Apr 5, 2024
afb7ff7
Create 1544. remove aA in string
dirwolf Apr 5, 2024
7d1f60c
Update Longest Common subsequence
dirwolf Apr 5, 2024
64fc49d
Add files via upload
dirwolf Apr 5, 2024
e4db443
Create Subset sum problem dp
dirwolf Apr 5, 2024
bb30dfd
Create hello
dirwolf Apr 9, 2024
d47ed82
Update and rename hello to dp :Subset sum equal to target
dirwolf Apr 9, 2024
cc6cc83
Update Minimal Cost of energy
dirwolf Apr 9, 2024
c10aabb
Create dp Maximum sum of non-adjacent subsequence
dirwolf Apr 9, 2024
c0f5483
Create 198. House Robber (dp)
dirwolf Apr 9, 2024
617662d
Create 2073. Time Needed to Buy Tickets
dirwolf Apr 9, 2024
222e383
Create CC: Alternating subarray prefix
dirwolf Apr 10, 2024
d28532a
Create CC: Count Subarrays INTDP01
dirwolf Apr 10, 2024
c72cc11
Create Count Inversions
dirwolf Apr 10, 2024
d72bf50
Create Merge sort
dirwolf Apr 10, 2024
7753294
Create Quick sort
dirwolf Apr 10, 2024
88f4483
Create dp: Ninja's Training (2d)
dirwolf Apr 11, 2024
fa6eab6
Create dp: Unique Paths
dirwolf Apr 13, 2024
83e16be
Create dp: UNique Paths
dirwolf Apr 13, 2024
51a5962
Update dp: UNique Paths
dirwolf Apr 13, 2024
d9801a0
Update dp: Ninja's Training (2d)
dirwolf Apr 13, 2024
0b61d2d
Update dp: UNique Paths
dirwolf Apr 13, 2024
793c98e
Create dp: 63. Unique Paths II
dirwolf Apr 13, 2024
5b4f7c0
Create dp : 64. Minimum Path Sum
dirwolf Apr 13, 2024
61744f4
create bool arr, traversal is i+=2 else i++
dirwolf Apr 14, 2024
74c2891
Create 404. Sum of Left Leaves
dirwolf Apr 15, 2024
0bac5ae
Create 221. Maximal Square
dirwolf Apr 15, 2024
a1360c6
Create Find Indexes of a subarray with given sum
dirwolf Apr 15, 2024
b0b5f5c
Sliding and 1 based
dirwolf Apr 15, 2024
1f09285
slliding with while or if else
dirwolf Apr 15, 2024
9ca5e49
Create String is palindrom( n$12S^7)
dirwolf Apr 15, 2024
c1c65c7
Create 129. Sum Root to Leaf Numbers
dirwolf Apr 15, 2024
0db12e2
Create Count distinct elements in every window
dirwolf Apr 16, 2024
df3cf03
standard sliding window
dirwolf Apr 16, 2024
7df0fe8
length is requires so,else mp[i] =i
dirwolf Apr 16, 2024
12c7c7a
here count is asked so mp[0]=1 and store , inc freq
dirwolf Apr 16, 2024
4d5c54b
Create 988. Smallest String Starting From Leaf
dirwolf Apr 17, 2024
7b54cd8
Create dp: Triangle 11
dirwolf Apr 18, 2024
8beaae1
Create Rohan's Love for matrix'
dirwolf Apr 19, 2024
07659f8
if rem found again then middle sum is zero
dirwolf Apr 19, 2024
a24cffd
Create 380. Insert Delete GetRandom O(1)
dirwolf Apr 19, 2024
5754b3c
Create dp: Maximum Path Sum in the matrix
dirwolf Apr 20, 2024
0725c18
variable start & end
dirwolf Apr 20, 2024
09e1813
Update dp: Maximum Path Sum in the matrix
dirwolf Apr 20, 2024
e9c6dd1
Create 213. House Robber II
dirwolf Apr 20, 2024
8d0ba02
Create 1992. Find All Groups of Farmland
dirwolf Apr 22, 2024
ffe3983
Create 3122. Minimum Number of Operations to Satisfy Conditions
dirwolf Apr 22, 2024
b0f5014
Create 752. Open the Lock
dirwolf Apr 22, 2024
e839b43
Create Circular tour
dirwolf Apr 23, 2024
aa3e944
Create Longest Common Substring
dirwolf Apr 24, 2024
9475bc1
Create dp:Partition Equal Subset Sum
dirwolf Apr 24, 2024
1bf73a4
Create Recersion: Merge two sorted linked lists
dirwolf Apr 25, 2024
85772bf
Create 733. Flood Fill (recur)
dirwolf Apr 25, 2024
adca0fc
Update dp:Partition Equal Subset Sum
dirwolf Apr 25, 2024
7df81eb
Create 39. Combination Sum
dirwolf Apr 26, 2024
74d9f2d
Create Minimum Number in a sorted rotated array
dirwolf Apr 27, 2024
a6c2976
Create 40. Combination Sum II
dirwolf Apr 27, 2024
f9e9cf4
Create Geek & Geekina Purchasing Ice Cream
dirwolf Apr 28, 2024
32d6c57
Create dp: minimum elements to reach target sum
dirwolf Apr 28, 2024
0cd0a2e
Create dp: Min element to reach target sum
dirwolf Apr 28, 2024
41a90a1
Create 3133. Minimum Array End
dirwolf May 1, 2024
c8a325f
Create 459. Reapeated substring pattern
dirwolf May 1, 2024
fdd2a70
Create 2000. Reverse Prefix of Word
dirwolf May 2, 2024
5a98b40
Create 2441. Largest Positive Integer That Exists With Its Negative
dirwolf May 2, 2024
d6c654e
Create 165. Compare Version Numbers
dirwolf May 4, 2024
fc3a6a1
Create 881. Boats to Save People
dirwolf May 4, 2024
92d13ce
Create 152. Maximum Product Subarray
dirwolf May 4, 2024
e1b7f67
Create w396: Minimum Length of Anagram Concatenation
dirwolf May 5, 2024
2c4ee9a
Create w396:Minimum Number of Operations to Make Word K-Periodic
dirwolf May 5, 2024
64ff474
Update w396: Minimum Length of Anagram Concatenation
dirwolf May 5, 2024
555458f
Create w396: valid Word_\
dirwolf May 5, 2024
9ca783e
Create gfg 148: Max splits
dirwolf May 6, 2024
62491e6
Create 2487. Remove Nodes From Linked List
dirwolf May 7, 2024
d22e7a8
Create 506. Relative Ranks
dirwolf May 9, 2024
0cfed3b
Create 3075. Maximize Happiness of Selected Children
dirwolf May 10, 2024
91a4e69
Create 51. N-Queens
dirwolf May 12, 2024
375315c
Create Minimum steps to destination**
dirwolf May 12, 2024
5ff8a0f
Create 1922. Count Good Numbers
dirwolf May 15, 2024
c7c1637
Create 1219. Path with Maximum Gold
dirwolf May 15, 2024
31934c9
Create 1162. As Far from Land as Possible
dirwolf May 16, 2024
50ed19d
Create (18May) 1325. Delete Leaves With a Given Value
dirwolf May 17, 2024
6ecc410
Create Mighty Friends(1400)
dirwolf May 19, 2024
7dcbe22
Create k largest elements
dirwolf May 19, 2024
12ec900
Create set & lower bound Special Array II
dirwolf May 19, 2024
1907203
Create 2262. Total Appeal of A String *
dirwolf May 20, 2024
8ccdad5
Create 70. Climbing Stairs(self)
dirwolf May 21, 2024
0a63070
Create Josephus problem
dirwolf May 21, 2024
3d334c6
Create Shortest Common Supersequence
dirwolf May 21, 2024
9062a17
Create LCS min no. of deletions and insertions
dirwolf May 21, 2024
e458272
Create Longest Repeating Subsequence
dirwolf May 21, 2024
9a1b585
Add files via upload
dirwolf May 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added C_Largest_Subsequence.bin
Binary file not shown.
52 changes: 52 additions & 0 deletions C_Largest_Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<iostream>
#include<bits/stdc++.h>
#define _test int _TEST; cin>>_TEST; while(_TEST--)
#define int long long
const int N = 100100;
using namespace std;
int32_t main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
vector<int>v;
int prev = 'a';
int count=0;
// we begin with smallest ,just in ints
for(int i =n-1;i>=0;i--){
if(s[i]>=prev){
// their inddices
v.push_back(i);
prev=s[i];

}
}
// prev me sabse bada jo hai vo hai
// ab hum uske count ko store karege
// aur use v me se remove karege
for(int i =0;i<n;i++){
if(prev == s[i]){
count++;
}
}
int m =v.size();
// size of larget subsequence
// after storing their indices we swp them
for(int i =0;i<m/2;i++){
swap(s[v[i]],s[v[m-i-1]]);
}
int ans = v.size()-count;
// now e check if sorteed or not
for(int i =1;i<n;i++){
if(s[i]<s[i-1]){
ans = -1;
break;
}
}
cout<<ans<<endl;
}
return 0;
}
30 changes: 30 additions & 0 deletions Day 10/2125. Number of Laser Beams in a Bank
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/?envType=daily-question&envId=2024-01-03
class Solution {
public:
int numberOfBeams(vector<string>& bank) {
int prevRowCount = 0;
int total = 0;

for (const std::string& row : bank) {
int curRowCount = calc(row);
if (curRowCount == 0)
continue;

total += curRowCount * prevRowCount;
prevRowCount = curRowCount;
}
return total;
}

private:
int calc(const std::string& s) {
int count = 0;
for(int i =0;i<s.size();i++){
if(s[i]=='1'){
count++;
}
}
return count;
}

};
28 changes: 28 additions & 0 deletions Day 10/Maximum matrix sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
https://leetcode.com/problems/maximum-matrix-sum/description/
Solution:class Solution {
public:
long long maxMatrixSum(vector<vector<int>>& matrix) {
int neg=0;
long long sum=0;
int mini=INT_MAX;
for (int i=0; i<matrix.size(); i++)
{
for (int j=0; j<matrix[0].size(); j++)
{
// using variables to instead of repeatedly accessing matrix[i][j], you can store the element in a variable to reduce the number of array accesses.
int element = matrix[i][j];
int absoluteValue = abs(element);
if (element < 0) {
neg += 1;
}
sum += absoluteValue;
if (absoluteValue < mini) {
mini = absoluteValue;
}
}
}
// why we subtracting twixe the min when neg count is odd?
// Because the sum of absolute values, absTotal was already added to the final sum here: absTotal += Math.abs(matrix[i][j]); That's why we have to subtract it twice.
return(neg%2)?sum-(2*mini):sum;
}
};
25 changes: 25 additions & 0 deletions Day 11/2870. Minimum Number of Operations to Make Array Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/?envType=daily-question&envId=2024-01-04
class Solution {
public:
int minOperations(std::vector<int>& nums) {
std::unordered_map<int, int> mp;

for (int i : nums) {
mp[i]++;
}

int ans = 0;

for (auto& kv : mp) {
int i = kv.second;

if (i == 1) {
return -1;
}

ans += (i+2) / 3;
}

return ans;
}
};
12 changes: 12 additions & 0 deletions Day 11/Asiign Cookies
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
https://leetcode.com/problems/assign-cookies/
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = 0;
for(int j=0; i<g.size() && j<s.size(); j++)
if(g[i]<=s[j]) i++;
return i;
}
};
1 change: 1 addition & 0 deletions Day 11/Longest disticnt char in string
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://www.geeksforgeeks.org/problems/longest-distinct-characters-in-string5848/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=bottom_sticky_on_article
29 changes: 29 additions & 0 deletions Day 11/Number of divisors
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
https://cses.fi/problemset/task/1713
#include <iostream>
#include <cmath>

using namespace std;

int countDivisors(int x) {
int divisors = 0;
for (int i = 1; i <= sqrt(x); ++i) {
if (x % i == 0) {
divisors += (i * i == x) ? 1 : 2;
}
}
return divisors;
}

int main() {
int n;
cin >> n;

for (int i = 0; i < n; ++i) {
int x;
cin >> x;
int divisors = countDivisors(x);
cout << divisors << endl;
}

return 0;
}
45 changes: 45 additions & 0 deletions Day 12/Daily LC (5 jan.)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
https://leetcode.com/problems/longest-increasing-subsequence/
// #include<bits/stc++.h>
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {

vector<int>temp;
int len=1;
temp.push_back(nums[0]);
for(int i =1;i<nums.size();i++){
if(nums[i]>temp.back()){
temp.push_back(nums[i]);
len++;
}
else{
auto it = lower_bound(temp.begin(), temp.end(), nums[i]);
int index = distance(temp.begin(), it);
temp[index]=nums[i];
}
}
return len;
}
// ##### THIS CODE IS FOR LONGEST CONSEQUENCE SUBSSEQUENCE ####
// if (nums.empty()) {
// return 0;
// }
// sort(nums.begin(),nums.end());
// int seqlen=1;
// int maxlen=1;
// for(int i =1;i<nums.size();i++){
// if(nums[i]==nums[i-1]+1){
// seqlen++;
// }
// else if(nums[i]==nums[i-1]){
// // case when duplicate found
// continue;
// }
// else{
// seqlen=1;
// }
// maxlen=max(maxlen,seqlen);
// }
// return maxlen;
// }
};
45 changes: 45 additions & 0 deletions Day 12/Range sum query 2d
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# how to resize the 2d vector outside the access specifier
matrix.resize(newRows, vector<int>(newCols, 0));

https://leetcode.com/problems/range-sum-query-2d-immutable/description/
Solution:class NumMatrix {
private:
int row,col;
vector<vector<int>>pf;
// vector<vector<int>matrix;
public:

NumMatrix(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return;
}

// this->matrix=matrix;
// pf.resize(n);
row = matrix.size();
col = matrix[0].size();
// pf.resize()
pf.resize(row+1,vector<int>(col+1,0));
// pf = vector<vector<int>>(row+1, vector<int>(col+1, 0));
for(int i =1;i<=row;i++){
for(int j =1;j<=col;j++){
// this is how the prefix 2d is made basically, 1-based indexing
pf[i][j]=matrix[i-1][j-1]+pf[i-1][j]+pf[i][j-1]-pf[i-1][j-1];
}
}
}

int sumRegion(int row1, int col1, int row2, int col2) {
// Calculate the sum of the region using the prefix sum array
// 1 based indexing used,whole cell sum - the prev -prev+corner
return pf[row2+1][col2+1] -
pf[row1][col2 + 1] -
pf[row2+1][col1] +
pf[row1][col1];
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
*/
26 changes: 26 additions & 0 deletions Day 12/Sum Range Query 1-D
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
https://leetcode.com/problems/range-sum-query-immutable/
class NumArray {
public:vector<int>pf;
vector<int>nums;
NumArray(vector<int>& nums) {
this->nums=nums;
int n = nums.size();
pf.resize(n);
prefixcal(nums,n);
}

int sumRange(int left, int right) {
return (left-1>=0)?pf[right]-pf[left-1]:pf[right];
}
void prefixcal(vector<int>&nums,int n){
for(int i=0;i<n;i++){
(i==0)?pf[i]=nums[i]:pf[i]=nums[i]+pf[i-1];
}
}
};

/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* int param_1 = obj->sumRange(left,right);
*/
37 changes: 37 additions & 0 deletions Day 13/ADD TWO NUMBERS (LL)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Problem link:https://leetcode.com/problems/add-two-numbers/
Solution:/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummyHead = new ListNode(0);
ListNode* tail =dummyHead;
int carry =0;
while(l1!=NULL || l2 !=NULL || carry!=0){
int digit1 = (l1!=NULL)?l1->val:0;
int digit2 = (l2!=NULL)?l2->val:0;
int sum =0;
sum = digit1 +digit2 +carry;
int digit = sum%10;
carry = sum/10;
ListNode* newNode = new ListNode(digit);
tail->next=newNode;
tail=tail->next;

// now traverse l1 and l2 towards right
l1 = (l1 != nullptr) ? l1->next : nullptr;
l2 = (l2 != nullptr) ? l2->next : nullptr;
}
ListNode* result = dummyHead->next;
delete dummyHead;
return result;
}
};
54 changes: 54 additions & 0 deletions Day 13/ADD TWO NUMBERS II
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
https://leetcode.com/problems/add-two-numbers-ii/description/
Soluton:/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverse(ListNode* l){
ListNode* prev =NULL;
ListNode* curr = l;
while(curr){
// first store the next before removing it
ListNode* nextNode = curr->next;
curr->next =prev;
prev=curr;
curr=nextNode;
}
return prev;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* rl1 = reverse(l1);
ListNode* rl2 = reverse(l2);

// As in this question we have to form a new list so create on love
ListNode* dummyhead = new ListNode(0);
ListNode* tail = dummyhead;
int carry =0;
while(rl1!=NULL || rl2 !=NULL || carry !=NULL){
int digit1 = (rl1!=NULL)?rl1->val:0;
int digit2 = (rl2!=NULL)?rl2->val:0;
int sum = digit1 +digit2 +carry;
int digit =sum%10;
carry =sum/10;;

ListNode* newNode = new ListNode(digit);
tail->next=newNode;
tail=tail->next;
// tail = newNode;
// now traverse the lists
rl1 = (rl1!=NULL)?rl1->next:NULL;
rl2 = (rl2!=NULL)?rl2->next:NULL;
}

ListNode* result = reverse(dummyhead->next);
delete(dummyhead);
return result;
}
};
Loading