-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'abhisek247767:main' into rotate_matrix_90
- Loading branch information
Showing
9 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
/* | ||
Problem Statement: | ||
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. | ||
Example 1: | ||
Input: nums = [1,2,3,1] | ||
Output: true | ||
Explanation: | ||
The element 1 occurs at the indices 0 and 3. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_map> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool Check_Duplicate(vector<int>& arr) { | ||
int n = arr.size(); | ||
unordered_map<int, int> un_map; | ||
for (int i = 0; i < n; i++) { | ||
un_map[arr[i]]++; | ||
} | ||
for (auto k : arr) { | ||
if (un_map[k] >= 2) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
int main() { | ||
Solution solution; | ||
vector<int> nums = { 19, 4, 19, 11 }; | ||
|
||
bool result = solution.Check_Duplicate(nums); | ||
if (result) { | ||
cout << "true" << endl; | ||
} | ||
else { | ||
cout << "false" << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int longestCommonSubsequence(string text1, string text2) { | ||
int m = text1.length(); | ||
int n = text2.length(); | ||
|
||
// Creating table | ||
int c[m+1][n+1]; | ||
for(int i = 0; i <= m; i++) { | ||
for(int j = 0; j <= n; j++) { | ||
c[i][j] = 0; | ||
} | ||
} | ||
|
||
for(int i = 1; i <= m; i++) { | ||
for(int j = 1; j <= n; j++) { | ||
if (text1[i-1] == text2[j-1]) { | ||
c[i][j] = c[i-1][j-1] + 1; | ||
} | ||
else if (c[i-1][j] >= c[i][j-1]) { | ||
c[i][j] = c[i-1][j]; | ||
} | ||
else { | ||
c[i][j] = c[i][j-1]; | ||
} | ||
} | ||
} | ||
|
||
return c[m][n]; | ||
} | ||
}; | ||
|
||
int main() { | ||
Solution solution; | ||
|
||
// Test Case 1 | ||
string text1 = "abcde"; | ||
string text2 = "ace"; | ||
cout << "Test Case 1 Output: " << solution.longestCommonSubsequence(text1, text2) << endl; | ||
|
||
// Test Case 2 | ||
text1 = "abc"; | ||
text2 = "abc"; | ||
cout << "Test Case 2 Output: " << solution.longestCommonSubsequence(text1, text2) << endl; | ||
|
||
// Test Case 3 | ||
text1 = "abc"; | ||
text2 = "def"; | ||
cout << "Test Case 3 Output: " << solution.longestCommonSubsequence(text1, text2) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Problem Statement: For a given m x n grid, where each cell has the following values : | ||
2 - represents a rotten orange | ||
1 - represents a Fresh orange | ||
0 - represents an Empty Cell | ||
Every minute, if a Fresh Orange is adjacent to a Rotten Orange in 4-direction ( upward, downwards, right, and left ) it becomes Rotten. | ||
Return the minimum number of minutes required such that none of the cells has a Fresh Orange. If it's not possible, return -1. | ||
Example: | ||
Input: grid - [ [2,1,1] , [0,1,1] , [1,0,1] ] | ||
Output: -1 | ||
Time Complexity: O ( n x n ) x 4 | ||
Space Complexity: O ( n x n ) | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int orangesRotting(vector<vector<int>>& grid) { | ||
if (grid.size() == 0) return 0; | ||
|
||
int n = grid.size(); // Number of rows | ||
int m = grid[0].size(); // Number of columns | ||
|
||
queue<pair<int, int>> rotpos; // Queue to store positions of rotten oranges | ||
int total = 0; // Total count of oranges (rotten + fresh) | ||
int cnt = 0; // Count of oranges rotten by the end | ||
|
||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
if (grid[i][j] == 2) rotpos.push({i, j}); // If rotten, push to rotpos queue | ||
if (grid[i][j] != 0) total++; // Count all non-empty cells as oranges | ||
} | ||
} | ||
|
||
// 4 possible movement directions (up, down, left, right) | ||
int dx[4] = {0, 0, 1, -1}; | ||
int dy[4] = {1, -1, 0, 0}; | ||
|
||
int minutes = 0; // Counter for minutes | ||
|
||
while (!rotpos.empty()) { | ||
int k = rotpos.size(); // Number of oranges to process for this day | ||
cnt += k; // Increase the rotten count by k | ||
|
||
// Process all oranges at the current min. level | ||
while (k--) { | ||
int x = rotpos.front().first, y = rotpos.front().second; | ||
rotpos.pop(); | ||
|
||
// Check for fresh oranges to rot | ||
for (int i = 0; i < 4; ++i) { | ||
int nx = x + dx[i], ny = y + dy[i]; | ||
|
||
// Skip if out of bounds or if not a fresh orange | ||
if (nx < 0 || ny < 0 || nx >= n || ny >= m || grid[nx][ny] != 1) continue; | ||
|
||
// Rot the fresh orange and add it to queue | ||
grid[nx][ny] = 2; | ||
rotpos.push({nx, ny}); | ||
} | ||
} | ||
|
||
// Increment minutes if there are more oranges to process in the queue | ||
if (!rotpos.empty()) minutes++; | ||
} | ||
|
||
return total == cnt ? minutes : -1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i]. | ||
Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability. | ||
If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5. | ||
*/ | ||
|
||
class Solution { | ||
public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) { | ||
double[] maxProb = new double[n]; | ||
maxProb[start_node] = 1.0; | ||
for (int i = 0; i < n - 1; i++) { | ||
boolean updated = false; | ||
for (int j = 0; j < edges.length; j++) { | ||
int u = edges[j][0]; | ||
int v = edges[j][1]; | ||
double prob = succProb[j]; | ||
|
||
if (maxProb[u] * prob > maxProb[v]) { | ||
maxProb[v] = maxProb[u] * prob; | ||
updated = true; | ||
} | ||
if (maxProb[v] * prob > maxProb[u]) { | ||
maxProb[u] = maxProb[v] * prob; | ||
vs updated = true; | ||
} | ||
} | ||
if (!updated) break; | ||
} | ||
return maxProb[end_node]; | ||
} | ||
} | ||
|
||
/* | ||
Example 1: | ||
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2 | ||
Output: 0.25000 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
//https://leetcode.com/problems/walking-robot-simulation/ | ||
|
||
//874. Walking Robot Simulation | ||
|
||
|
||
class Solution { | ||
public int robotSim(int[] commands, int[][] obstacles) { | ||
// Directions: north, east, south, west | ||
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; | ||
int x = 0, y = 0; // Robot's starting position | ||
int dir = 0; // Start facing north | ||
int maxDistSquared = 0; // Maximum distance squared from the origin | ||
|
||
// Store obstacles as a set of strings "x,y" | ||
Set<String> obstacleSet = new HashSet<>(); | ||
for (int[] obstacle : obstacles) { | ||
obstacleSet.add(obstacle[0] + "," + obstacle[1]); | ||
} | ||
|
||
// Process each command | ||
for (int command : commands) { | ||
if (command == -2) { // Turn left | ||
dir = (dir + 3) % 4; // Equivalent to turning left 90 degrees | ||
} else if (command == -1) { // Turn right | ||
dir = (dir + 1) % 4; // Equivalent to turning right 90 degrees | ||
} else { // Move forward | ||
for (int i = 0; i < command; i++) { | ||
int nextX = x + directions[dir][0]; | ||
int nextY = y + directions[dir][1]; | ||
// Check if the next position is an obstacle | ||
if (!obstacleSet.contains(nextX + "," + nextY)) { | ||
x = nextX; | ||
y = nextY; | ||
// Update the maximum distance squared | ||
maxDistSquared = Math.max(maxDistSquared, x * x + y * y); | ||
} else { | ||
// Stop moving if there's an obstacle | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return maxDistSquared; | ||
} | ||
} | ||
|
||
/* | ||
Example 1: | ||
Input: commands = [4,-1,3], obstacles = [] | ||
Output: 25 | ||
Explanation: | ||
The robot starts at (0, 0): | ||
Move north 4 units to (0, 4). | ||
Turn right. | ||
Move east 3 units to (3, 4). | ||
The furthest point the robot ever gets from the origin is (3, 4), which squared is 32 + 42 = 25 units away. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
public class MergeSort { | ||
// Merge two subarrays of arr[]. | ||
// First subarray is arr[l..m] | ||
// Second subarray is arr[m+1..r] | ||
public static void merge(int arr[], int l, int m, int r) { | ||
int n1 = m - l + 1; | ||
int n2 = r - m; | ||
|
||
// Create temporary arrays | ||
int L[] = new int[n1]; | ||
int R[] = new int[n2]; | ||
|
||
// Copy data to temporary arrays L[] and R[] | ||
for (int i = 0; i < n1; i++) | ||
L[i] = arr[l + i]; | ||
for (int j = 0; j < n2; j++) | ||
R[j] = arr[m + 1 + j]; | ||
|
||
// Merge the temporary arrays | ||
int i = 0, j = 0; | ||
int k = l; | ||
|
||
// Merging back into arr[l..r] | ||
while (i < n1 && j < n2) { | ||
if (L[i] <= R[j]) { | ||
arr[k] = L[i]; | ||
i++; | ||
} else { | ||
arr[k] = R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
|
||
// Copy the remaining elements of L[], if any | ||
while (i < n1) { | ||
arr[k] = L[i]; | ||
i++; | ||
k++; | ||
} | ||
|
||
// Copy the remaining elements of R[], if any | ||
while (j < n2) { | ||
arr[k] = R[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
|
||
// Main function that sorts arr[l..r] using merge() | ||
public static void sort(int arr[], int l, int r) { | ||
if (l < r) { | ||
// Find the middle point | ||
int m = l + (r - l) / 2; | ||
|
||
// Recursively sort first and second halves | ||
sort(arr, l, m); | ||
sort(arr, m + 1, r); | ||
|
||
// Merge the sorted halves | ||
merge(arr, l, m, r); | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
int arr[] = {12, 11, 13, 5, 6, 7}; | ||
|
||
// Calling the sort function | ||
sort(arr, 0, arr.length - 1); | ||
|
||
// Print sorted array | ||
System.out.println("Sorted array:"); | ||
for (int num : arr) { | ||
System.out.print(num + " "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
''' | ||
Question : We are given an array with consecutive natural numbers , with one number missing | ||
We have to return that number . | ||
Example : Input = a =[1,2,3,4,5,6,8,9,10] | ||
output = 7 | ||
Approach : There exists a very simple approach to this problem . But , overthinking can mess it up, | ||
Sum= (n(n+1))/2 is the formula for sum of n natural numbers , if we subtract then sum of | ||
elements of the given array with this then the result will be the remaining number . | ||
where , n = len(a) | ||
''' | ||
class Solution: | ||
def missingNumber(self, nums: list[int]) -> int: | ||
n = len(nums) | ||
expected_sum = n * (n + 1) // 2 | ||
actual_sum = sum(nums) | ||
return expected_sum - actual_sum |
Oops, something went wrong.