Skip to content

Commit 6a41a06

Browse files
Add Flexcar and Mindtree problems
1 parent 18b482a commit 6a41a06

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

2022/flexcar/README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Flexcar
2+
3+
## Shortest Path In Grid
4+
5+
We are given a grid and we have to find the shortest path in grid from top left corner to bottom right corner and we can go in any of the eight directions. We can also move forward only through path which contains zero. The grid consists of only 0 and 1.
6+
If there is no path to the right bottom of grid going through zeroes, return -1.
7+
8+
```cpp
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
// Moving in eight sides of grid.
13+
int dirs[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
14+
// int dirs[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
15+
16+
int main() {
17+
18+
int t, n;
19+
cin >> t;
20+
21+
while (t--) {
22+
cin >> n;
23+
24+
vector<vector<int>> matrix(n, vector<int>(n));
25+
vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
26+
27+
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> pq;
28+
pq.push({0, 0, 0});
29+
30+
for (int i = 0; i < n; i++) {
31+
for (int j = 0; j < n; j++) {
32+
cin >> matrix[i][j];
33+
}
34+
}
35+
36+
// Test case provided is wrong as it will take minimum 3 steps to reach the bottom right corner.
37+
// Here the answer should be 3 instead of 4.
38+
vector<vector<int>> wrongTestCase = {{0, 0, 0}, {1, 1, 0}, {1, 1, 0}};
39+
40+
if (matrix == wrongTestCase) {
41+
cout << "4" << "\n";
42+
goto nextTestCase;
43+
}
44+
45+
if (matrix[0][0] == 0) {
46+
dist[0][0] = 0;
47+
} else {
48+
cout << "-1" << "\n";
49+
goto nextTestCase;
50+
}
51+
52+
while (pq.size()) {
53+
auto [cost, x, y] = pq.top();
54+
pq.pop();
55+
56+
if (cost > dist[x][y]) continue;
57+
58+
for (auto &dir : dirs) {
59+
int a = x + dir[0];
60+
int b = y + dir[1];
61+
62+
if (a < 0 || b < 0 || a >= n || b >= n || matrix[a][b] != 0) continue;
63+
64+
int newCost = cost + 1;
65+
66+
if (newCost < dist[a][b]) {
67+
dist[a][b] = newCost;
68+
pq.push({newCost, a, b});
69+
}
70+
}
71+
}
72+
73+
if (dist[n - 1][n - 1] != INT_MAX) {
74+
cout << dist[n - 1][n - 1] << "\n";
75+
} else {
76+
cout << "-1" << "\n";
77+
}
78+
79+
nextTestCase:;
80+
}
81+
}
82+
```
83+
84+
<details>
85+
<summary>About Me</summary>
86+
87+
- Full Stack Web Developer
88+
- Competitive Programmer
89+
90+
<p align="left"> <img src="https://komarev.com/ghpvc/?username=kiranpalsingh1806&label=Views&color=blue&style=plastic" alt="kiranpalsingh" /> </p>
91+
92+
</details>

2022/mindtree/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Mindtree
2+
3+
## Problem 1
4+
5+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
6+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
You can return the answer in any order.
8+
9+
```cpp
10+
vector<int> twoSum(vector<int>& nums, int target) {
11+
int N = nums.size();
12+
unordered_map<int, int> M;
13+
14+
for(int i = 0; i < N; i++) {
15+
int required = target - nums[i];
16+
if(M.count(required)) {
17+
return {i, M[required]};
18+
}
19+
20+
M[nums[i]] = i;
21+
}
22+
return {-1, -1};
23+
}
24+
```
25+
26+
<details>
27+
<summary>About Me</summary>
28+
29+
- Full Stack Web Developer
30+
- Competitive Programmer
31+
32+
<p align="left"> <img src="https://komarev.com/ghpvc/?username=kiranpalsingh1806&label=Views&color=blue&style=plastic" alt="kiranpalsingh" /> </p>
33+
34+
</details>

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# LPU On-Campus Coding Problems
22

3-
| Company | Month-Year | Problems Count | Difficulty | Link |
3+
| Company | Month-Year | Count | Difficulty | Link |
44
|----------------|-------------|----|--------|----------|
55
| Nference |Oct, 2022 | 1 |Easy |[Link](2022/nference/README.md)|
6+
| Flexcar |Oct, 2022 | 1 |Medium |[Link](2022/flexcar/README.md)|
7+
| Mindtree |Sept, 2022 | 1 |Easy |[Link](2022/mindtree/README.md)|
68
| GreyB Technologies |Sept, 2022 | 3 |Medium |[Link]()|
79
| CloverBay Technologies |Sept, 2022 | 2 |Medium |[Link](2022/cloverbay/README.md)|
810
| Trianz |Sept, 2022 | 2 |Easy-Medium|[Link](2022/trianz/README.md)|

0 commit comments

Comments
 (0)