Skip to content

Commit cec6743

Browse files
authored
Merge branch 'codemistic:main' into patch#2
2 parents fb6aa5d + 93fac0b commit cec6743

14 files changed

+435
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int reverse(int x) {
4+
long ans = 0;
5+
6+
while (x) {
7+
ans = ans * 10 + x % 10;
8+
x /= 10;
9+
}
10+
11+
return (ans < INT_MIN || ans > INT_MAX) ? 0 : ans;
12+
}
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string countAndSay(int n) {
4+
string ans = "1";
5+
6+
while (--n) {
7+
string next;
8+
for (int i = 0; i < ans.length(); ++i) {
9+
int count = 1;
10+
while (i + 1 < ans.length() && ans[i] == ans[i + 1]) {
11+
++count;
12+
++i;
13+
}
14+
next += to_string(count) + ans[i];
15+
}
16+
ans = move(next);
17+
}
18+
19+
return ans;
20+
}
21+
};

CPP/array-2d/search-a-2d-matrix.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
4+
int m = matrix.size(), n = matrix[0].size();
5+
6+
int i=0,j=n-1;
7+
while(i<m && j>=0){
8+
if(target==matrix[i][j]){
9+
10+
return true;
11+
}
12+
else if(target<matrix[i][j]){
13+
j--;
14+
} else {
15+
i++;
16+
}
17+
}
18+
return false;
19+
}
20+
};

CPP/graph_tree/Dijkstra_Algorithm.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
int n=5,m=6,source=1;
5+
vector<pair<int,int> > g[n+1]; // assuming 1 based indexing of graph
6+
// Constructing the graph
7+
g[1].push_back({2,2});
8+
g[1].push_back({4,1});
9+
g[2].push_back({1,2});
10+
g[2].push_back({5,5});
11+
g[2].push_back({3,4});
12+
g[3].push_back({2,4});
13+
g[3].push_back({4,3});
14+
g[3].push_back({5,1});
15+
g[4].push_back({1,1});
16+
g[4].push_back({3,3});
17+
g[5].push_back({2,5});
18+
g[5].push_back({3,1});
19+
// Dijkstra's algorithm begins from here
20+
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int>>> pq;
21+
vector<int> distTo(n+1,INT_MAX);//1-indexed array for calculating shortest paths
22+
distTo[source] = 0;
23+
pq.push(make_pair(0,source)); // (dist,source)
24+
while( !pq.empty() ){
25+
int dist = pq.top().first;
26+
int prev = pq.top().second;
27+
pq.pop();
28+
vector<pair<int,int> >::iterator it;
29+
for( it = g[prev].begin() ; it != g[prev].end() ; it++){
30+
int next = it->first;
31+
int nextDist = it->second;
32+
if( distTo[next] > distTo[prev] + nextDist){
33+
distTo[next] = distTo[prev] + nextDist;
34+
pq.push(make_pair(distTo[next], next));
35+
}
36+
}
37+
}
38+
cout << "The distances from source " << source << " are : \n";
39+
for(int i = 1 ; i<=n ; i++) cout << distTo[i] << " ";
40+
cout << "\n";
41+
return 0;
42+
}
43+
44+
/*
45+
Output:
46+
47+
The distances from source 1 are :
48+
0 2 4 1 5
49+
50+
Time Complexity: O((N+E)*logN). Going through N nodes and E edges and log N for priority queue
51+
52+
Space Complexity: O(N). Distance array and priority queue
53+
*/
54+
55+
////contributed by Sourav Naskar
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//leet-code discuss link :
2+
// https://leetcode.com/problems/find-k-closest-elements/discuss/2637568/Very-Simple-O(N)-solution-using-sliding-window
3+
4+
class Solution {
5+
public:
6+
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
7+
//lets try sliding window
8+
//here sum = sum of all differences between elements and x.
9+
//sum to store all values in your window of size k
10+
int sum =0;
11+
//indices of window with minimum sum
12+
pair<int,int> win = {0,k-1};
13+
for(int i =0;i<k;i++){
14+
sum+=abs(arr[i]-x);
15+
}
16+
int mn = sum;
17+
//traverse all windows and find one with minimum sum
18+
for(int i = 1;i<=arr.size()-k;i++){
19+
//remove first element
20+
sum-=abs(arr[i-1]-x);
21+
//add new element
22+
sum+=abs(arr[i+k-1]-x);
23+
//update mn and win in case a new min window is found
24+
if(mn>sum){
25+
mn = sum;
26+
win = {i,i+k-1};
27+
}
28+
}
29+
//store the result in res and return.
30+
vector<int> res;
31+
for(int i = win.first;i<=win.second;i++){
32+
res.push_back(arr[i]);
33+
}
34+
return res;
35+
}
36+
};

Dynamic Programming/Edit Distance.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 72. Edit Distance (LeetCode Hard)
2+
// https://leetcode.com/problems/edit-distance/
3+
4+
class Solution {
5+
public:
6+
7+
int solve(int i1, int i2, string word1, string word2, vector<vector<int>>&dp)
8+
{
9+
//base case
10+
if (i1 == -1 && i2 == -1)
11+
{
12+
return 0;
13+
}
14+
if (i1 == -1)
15+
{
16+
return i2 + 1;
17+
}
18+
if (i2 == -1)
19+
{
20+
return i1 + 1;
21+
}
22+
23+
if (dp[i1][i2] != -1)
24+
return dp[i1][i2];
25+
26+
//recursion
27+
28+
if (word1[i1] == word2[i2])
29+
{
30+
return solve(i1 - 1, i2 - 1, word1, word2, dp);
31+
}
32+
33+
int insert = solve(i1 - 1, i2 , word1, word2, dp);
34+
int del = solve(i1, i2 - 1 , word1, word2, dp);
35+
int replace = solve(i1 - 1, i2 - 1 , word1, word2, dp);
36+
37+
dp[i1][i2] = 1 + min(insert, min(del, replace));
38+
return dp[i1][i2];
39+
}
40+
41+
int minDistance(string word1, string word2) {
42+
43+
int l1 = word1.size();
44+
int l2 = word2.size();
45+
vector<vector<int>>dp(l1, vector<int>(l2, -1));
46+
47+
return solve(l1 - 1, l2 - 1, word1, word2, dp);
48+
49+
}
50+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://leetcode.com/problems/maximal-square/
2+
// Maximal Square
3+
4+
class Solution {
5+
public:
6+
7+
8+
int solveTab(vector<vector<char>>& matrix, int &maxi)
9+
{
10+
vector<int>prev(matrix[0].size() + 1, 0);
11+
vector<int>curr(matrix[0].size() + 1);
12+
13+
for (int row = matrix.size() - 1; row >= 0; row --)
14+
{
15+
for (int column = matrix[0].size() - 1; column >= 0; column--)
16+
{
17+
int right = curr[column + 1];
18+
int diag = prev[column + 1];
19+
int down = prev[column];
20+
21+
if (matrix[row][column] == '1')
22+
{
23+
int ans = 1 + min(right, min(diag, down));
24+
maxi = max(maxi , ans);
25+
curr[column] = ans;
26+
27+
}
28+
else
29+
{
30+
curr[column] = 0;
31+
}
32+
}
33+
prev = curr;
34+
}
35+
return 0;
36+
}
37+
38+
int maximalSquare(vector<vector<char>>& matrix) {
39+
40+
int maxi = 0;
41+
solveTab(matrix, maxi);
42+
return maxi * maxi;
43+
}
44+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 42. Trapping Rain Water (Leetcode)
2+
// https://leetcode.com/problems/trapping-rain-water/
3+
4+
class Solution {
5+
public:
6+
int trap(vector<int>& height) {
7+
8+
int n = height.size();
9+
vector<int>leftHeight(n, -1), rightHeight(n, -1);
10+
int ans = 0;
11+
12+
for (int i = 1; i < n; i++)
13+
{
14+
leftHeight[i] = max(leftHeight[i - 1], height[i - 1]);
15+
rightHeight[n - i - 1] = max(rightHeight[n - i], height[n - i]);
16+
}
17+
18+
for (int i = 1; i < n; i++)
19+
{
20+
int x = min(leftHeight[i], rightHeight[i]) - height [i];
21+
if (x > 0)
22+
{
23+
ans += x;
24+
}
25+
}
26+
27+
return ans;
28+
}
29+
};

Java/QuickSort_Array.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/* Program to Sort an Array Using the QuickSort Sorting Algorithm */
3+
4+
public class QuickSort_Array {
5+
public static void main(String[] args) {
6+
int arr[] = {1000,6,3,5,2,8,9,12,200};
7+
int n = arr.length;
8+
quickSort(arr, 0, n-1);
9+
10+
for (int i = 0; i < arr.length; i++) {
11+
System.out.print(arr[i]+" ");
12+
}
13+
}
14+
15+
public static void quickSort(int arr[], int low, int high) {
16+
if(low < high)
17+
{
18+
int pindex = partition(arr, low, high);
19+
20+
quickSort(arr, low, pindex-1);
21+
quickSort(arr, pindex+1, high);
22+
}
23+
}
24+
public static int partition(int arr[], int low, int high) {
25+
int piv = arr[high];
26+
int i=low-1;
27+
28+
for (int j = low; j < high; j++) {
29+
if(arr[j]<piv)
30+
{
31+
i++;
32+
int temp = arr[i];
33+
arr[i] = arr[j];
34+
arr[j] = temp;
35+
}
36+
}
37+
i++;
38+
int temp = arr[i];
39+
arr[i] = piv;
40+
arr[high] = temp;
41+
42+
return i;
43+
}
44+
}

Python/HuffmanCodingAlgo.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import heapq
2+
3+
class node:
4+
def __init__(self, freq, symbol, left=None, right=None):
5+
6+
self.freq = freq
7+
8+
self.symbol = symbol
9+
10+
self.left = left
11+
12+
self.right = right
13+
14+
self.huff = ''
15+
16+
def __lt__(self, nxt):
17+
return self.freq < nxt.freq
18+
19+
def printNodes(node, val=''):
20+
21+
newVal = val + str(node.huff)
22+
23+
if(node.left):
24+
printNodes(node.left, newVal)
25+
if(node.right):
26+
printNodes(node.right, newVal)
27+
28+
if(not node.left and not node.right):
29+
print(f"{node.symbol} -> {newVal}")
30+
31+
32+
chars = ['a', 'b', 'c', 'd', 'e', 'f']
33+
34+
freq = [ 4, 7, 12, 14, 35, 72]
35+
36+
nodes = []
37+
38+
for x in range(len(chars)):
39+
heapq.heappush(nodes, node(freq[x], chars[x]))
40+
41+
while len(nodes) > 1:
42+
43+
left = heapq.heappop(nodes)
44+
right = heapq.heappop(nodes)
45+
46+
left.huff = 0
47+
right.huff = 1
48+
49+
newNode = node(left.freq+right.freq, left.symbol+right.symbol, left, right)
50+
51+
heapq.heappush(nodes, newNode)
52+
53+
printNodes(nodes[0])

Python/Nestedlist.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def solution():
2+
result =[]
3+
scores_list = []
4+
for _ in range(int(input("Number of students?? "))):
5+
name = str(input("Students' name? ").strip())
6+
score = float(input("{}'s Marks ".format(name)))
7+
result+=[[name,score]]
8+
scores_list+=[score]
9+
10+
b = sorted(list(set(scores_list)))[1]
11+
12+
for a,c in sorted(result):
13+
if c==b:
14+
print(a)
15+
16+
if __name__ == '__main__':
17+
solution()

0 commit comments

Comments
 (0)