Skip to content

Commit 84924e2

Browse files
committed
Merge branch 'smv1999:master' into manny
2 parents d7de999 + c886d8e commit 84924e2

27 files changed

+1355
-4
lines changed

2D Arrays/Set Matrix Zero.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*______________________________________________________________________PROBLEM___________________________________________________________*/
2+
3+
/*Given an m x n matrix. If an element is 0, set its entire row and column to 0. Do it in-place.
4+
Follow up:
5+
6+
A straight forward solution using O(mn) space is probably a bad idea.
7+
A simple improvement uses O(m + n) space, but still not the best solution.
8+
Could you devise a constant space solution?
9+
10+
EXAMPLE 1:
11+
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
12+
Output: [[1,0,1],[0,0,0],[1,0,1]]
13+
14+
EXAMPLE 2:
15+
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
16+
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
17+
18+
19+
20+
Constraints:
21+
22+
m == matrix.length
23+
n == matrix[0].length
24+
1 <= m, n <= 200
25+
-231 <= matrix[i][j] <= 231 - 1
26+
*/
27+
28+
29+
class Solution {
30+
public:
31+
void setZeroes(vector<vector<int>>& m) {
32+
set<int>row,col;
33+
34+
for(int i=0;i<m.size();i++){
35+
for(int j=0;j<m[0].size();j++){
36+
if(m[i][j]==0){
37+
row.insert(i);
38+
col.insert(j);
39+
}
40+
}
41+
}
42+
43+
44+
for(auto it=row.begin();it!=row.end();it++){
45+
for(int j=0;j<m[0].size();j++){
46+
m[*it][j]=0;
47+
}
48+
49+
}
50+
for(auto it=col.begin();it!=col.end();it++){
51+
for(int j=0;j<m.size();j++){
52+
m[j][*it]=0;
53+
}
54+
}
55+
}

DSA 450 GFG/BuyandSell.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Link to the problem : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
class Solution:
4+
def maxProfit(self, prices):
5+
min_so_far = prices[0]
6+
max_profit = 0
7+
n = len(prices)
8+
for i in prices :
9+
min_so_far = min(min_so_far , i)
10+
profit = i - min_so_far
11+
max_profit = max(max_profit , profit)
12+
return max_profit

DSA 450 GFG/CountAndSay.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Problem : https://leetcode.com/problems/count-and-say/
2+
3+
# Input: n = 4
4+
# Output: "1211"
5+
# Explanation:
6+
# countAndSay(1) = "1"
7+
# countAndSay(2) = say "1" = one 1 = "11"
8+
# countAndSay(3) = say "11" = two 1's = "21"
9+
# countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
10+
11+
# Initialize counter = 1 to store the count of every element
12+
# Initialize o/p string as ret = ""
13+
# If the previous element in the string is equal to the current element in the string , increament the counter
14+
# else Concatenate the count and the previous element of the string in ret
15+
16+
# Return ret
17+
18+
class Solution:
19+
def countAndSay(self, n):
20+
if (n == 1):
21+
return ("1")
22+
23+
s = self.countAndSay(n-1)
24+
25+
ret = ""
26+
cnt = 1
27+
i = 1
28+
while i < len(s) + 1:
29+
if i < len(s) and s[i] == s[i-1]:
30+
cnt += 1
31+
else:
32+
ret += str(cnt) + str(s[i-1])
33+
cnt = 1
34+
i += 1
35+
36+
return (ret)

DSA 450 GFG/PrintAnagrams.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# Problem : https://practice.geeksforgeeks.org/problems/print-anagrams-together/1
3+
4+
5+
# Input:
6+
# N = 5
7+
# words[] = {act,god,cat,dog,tac}
8+
# Output:
9+
# god dog
10+
# act cat tac
11+
# Explanation:
12+
# There are 2 groups of
13+
# anagrams "god", "dog" make group 1.
14+
# "act", "cat", "tac" make group 2.
15+
16+
from collections import defaultdict
17+
18+
def Anagrams(words,n):
19+
'''
20+
words: list of word
21+
n: no of words
22+
return : list of group of anagram {list will be sorted in driver code (not word in grp)}
23+
'''
24+
25+
#code here
26+
anagrams = defaultdict(list)
27+
28+
for word in words:
29+
anagrams["".join(sorted(word))].append(word)
30+
31+
return anagrams.values()
32+
33+
34+
# Driver Code
35+
36+
if __name__ =='__main__':
37+
t= int(input())
38+
for tcs in range(t):
39+
n= int(input())
40+
words=input().split()
41+
42+
ans=Anagrams(words,n)
43+
44+
for grp in sorted(ans):
45+
for word in grp:
46+
print(word,end=' ')
47+
print()
48+
49+
50+
51+
# Used default dict from collections module . It does not raise key value error

DSA 450 GFG/TrapRainWater.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Problem : https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1
2+
3+
class Solution:
4+
def trappingWater(self, arr,n):
5+
#Code here
6+
left = [0]*n
7+
right = [0]*n
8+
9+
left[0] = arr[0]
10+
right[n-1] = arr[n-1]
11+
trap_sum = 0
12+
13+
for i in range (1 , n):
14+
left[i] = max(left[i-1] , arr[i])
15+
for i in range(n-2 , -1 , -1):
16+
right[i] = max(arr[i] , right[i+1])
17+
for i in range (0 , n):
18+
trap_sum += min(left[i] , right[i]) - arr[i]
19+
20+
return trap_sum
21+
#{
22+
# Driver Code Starts
23+
#Initial Template for Python 3
24+
25+
import math
26+
27+
28+
29+
def main():
30+
T=int(input())
31+
while(T>0):
32+
33+
n=int(input())
34+
35+
arr=[int(x) for x in input().strip().split()]
36+
obj = Solution()
37+
print(obj.trappingWater(arr,n))
38+
39+
40+
T-=1
41+
42+
43+
if __name__ == "__main__":
44+
main()
45+
46+
47+
48+
# } Driver Code Ends

DSA 450 GFG/chocolate_distribution.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Problem : https://practice.geeksforgeeks.org/problems/chocolate-distribution-problem3825/1#
2+
3+
#Given an array A[ ] of positive integers of size N, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are M students, the task is to distribute chocolate packets among M students such that :
4+
#1. Each student gets exactly one packet.
5+
#2. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum.
6+
7+
8+
class Solution:
9+
10+
def findMinDiff(self, A,N,M):
11+
12+
# code here
13+
if(N == 0 or M == 0):
14+
return 0
15+
elif (N < M):
16+
return -1
17+
else:
18+
A.sort()
19+
min_diff = A[N-1] - A[0]
20+
for i in range(0 , N - M + 1):
21+
min_diff = min(min_diff , A[i+M-1] - A[i])
22+
return min_diff
23+
24+
25+
#{
26+
# Driver Code Starts
27+
#Initial Template for Python 3
28+
29+
if __name__ == '__main__':
30+
31+
t = int(input())
32+
33+
for _ in range(t):
34+
N = int(input())
35+
A = [int(x) for x in input().split()]
36+
M = int(input())
37+
38+
39+
solObj = Solution()
40+
41+
print(solObj.findMinDiff(A,N,M))
42+
# } Driver Code Ends

DSA 450 GFG/max_and_min_of_array.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Maximum and minimum of an array using minimum number of comparisons - Python
2+
3+
def getMinMax(arr):
4+
5+
n = len(arr)
6+
7+
# If array has even number of elements then initialize the first two elements as minimum and maximum
8+
if(n % 2 == 0):
9+
maximum = max(arr[0], arr[1])
10+
minimum = min(arr[0], arr[1])
11+
12+
# set the starting index for loop
13+
i = 2
14+
15+
# If array has odd number of elements then initialize the first element as minimum and maximum
16+
else:
17+
maximum = minimum = arr[0]
18+
19+
# set the starting index for loop
20+
i = 1
21+
22+
# In the while loop, pick elements in pair and compare the pair with max and min so far
23+
while(i < n - 1):
24+
if arr[i] < arr[i + 1]:
25+
maximum = max(maximum, arr[i + 1])
26+
minimum = min(minimum, arr[i])
27+
else:
28+
maximum = max(maximum, arr[i])
29+
minimum = min(minimum, arr[i + 1])
30+
31+
# Increment the index by 2 as two elements are processed in loop
32+
i += 2
33+
34+
return (maximum, minimum)
35+
36+
# Driver Code
37+
if __name__ =='__main__':
38+
39+
arr = [9, 1, 45, 2, 330, 3]
40+
maximum, minimum = getMinMax(arr)
41+
print("Minimum element is", minimum)
42+
print("Maximum element is", maximum)
43+
44+

DSA 450 GFG/merge_intervals.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# Link for the problem : https://leetcode.com/problems/merge-intervals/
3+
4+
# Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
5+
# Output: [[1,6],[8,10],[15,18]]
6+
# Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
7+
8+
# Create a list merged to store the merged intervals
9+
# Condition for merging : If the first index of current interval is less than last index of previous interval
10+
# If condition satisfies , replace the last index of previous interval with the maximum between current and previous interval's last index
11+
# If not , then simply append the interval in the merged list
12+
13+
14+
15+
class Solution:
16+
def merge(self, intervals):
17+
18+
intervals.sort(key = lambda x : x[0])
19+
merged = []
20+
21+
for interval in intervals :
22+
23+
if not merged or merged[-1][1] < interval[0] :
24+
merged.append(interval)
25+
else :
26+
merged[-1][1] = max(merged[-1][1] , interval[1])
27+
28+
return merged
29+
30+

DSA 450 GFG/move_all_neg_numbers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
def rearrange (arr , n ):
4+
j = 0
5+
for i in range(0 , n) :
6+
if(arr[i] < 0):
7+
temp = arr[i]
8+
arr[i] = arr[j]
9+
arr[j] = temp
10+
j = j + 1
11+
print(arr)
12+
13+
#Driver code
14+
sequence = [1 , 3, - 6 , 9 , -3 , -1]
15+
length = len(sequence)
16+
print(sequence.sort)
17+
rearrange(sequence , length)

DSA 450 GFG/reverse_an_array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write a program to reverse an array
2+
# Given an array (or string), the task is to reverse the array/string.
3+
# Examples :
4+
5+
# Input : arr[] = [1, 2, 3]
6+
# Output : arr[] = [3, 2, 1]
7+
# Input : arr[] = [4, 5, 1, 2]
8+
# Output : arr[] = [2, 1, 5, 4]
9+
10+
11+
12+
13+
# Reverse of an array can be implented in Python using the List slicing
14+
15+
def reverseList(a):
16+
print( a[::-1])
17+
18+
# Driver Code
19+
arr = [4,5,1,2]
20+
print(arr)
21+
print("Reverse : ")
22+
reverseList(arr)

0 commit comments

Comments
 (0)