Skip to content

Commit 5e96819

Browse files
Add files via upload
1 parent b7854c1 commit 5e96819

File tree

7 files changed

+318
-0
lines changed

7 files changed

+318
-0
lines changed

Greedy/AssignMiceToHoles.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
There are N Mice and N holes are placed in a straight line.
3+
Each hole can accomodate only 1 mouse.
4+
A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consumes 1 minute.
5+
Assign mice to holes so that the time when the last mouse gets inside a hole is minimized.
6+
7+
Example:
8+
9+
positions of mice are:
10+
4 -4 2
11+
positions of holes are:
12+
4 0 5
13+
14+
Assign mouse at position x=4 to hole at position x=4 : Time taken is 0 minutes
15+
Assign mouse at position x=-4 to hole at position x=0 : Time taken is 4 minutes
16+
Assign mouse at position x=2 to hole at position x=5 : Time taken is 3 minutes
17+
After 4 minutes all of the mice are in the holes.
18+
19+
Since, there is no combination possible where the last mouse's time is less than 4,
20+
answer = 4.
21+
Input:
22+
23+
A : list of positions of mice
24+
B : list of positions of holes
25+
Output:
26+
27+
single integer value
28+
NOTE: The final answer will fit in a 32 bit signed integer.
29+
30+
LINK: https://www.interviewbit.com/problems/assign-mice-to-holes/
31+
*/
32+
33+
int Solution::mice(vector<int> &A, vector<int> &B)
34+
{
35+
int n = A.size();
36+
37+
sort(A.begin(), A.end());
38+
sort(B.begin(), B.end());
39+
40+
int ans = -1;
41+
42+
for(int i=0;i<n;i++)
43+
ans = max(ans, abs(A[i]-B[i]));
44+
45+
return ans;
46+
}

Greedy/Bulbs.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times.
3+
4+
Note : 0 represents the bulb is off and 1 represents the bulb is on.
5+
6+
Example:
7+
8+
Input : [0 1 0 1]
9+
Return : 4
10+
11+
Explanation :
12+
press switch 0 : [1 0 1 0]
13+
press switch 1 : [1 1 0 1]
14+
press switch 2 : [1 1 1 0]
15+
press switch 3 : [1 1 1 1]
16+
17+
LINK: https://www.interviewbit.com/problems/bulbs/
18+
*/
19+
20+
int Solution::bulbs(vector<int> &A)
21+
{
22+
int n = A.size();
23+
int sum = 0;
24+
int temp = 1;
25+
26+
for(int i=0;i<n;i++)
27+
{
28+
if(A[i]^temp)
29+
{
30+
sum++;
31+
temp ^= 1;
32+
}
33+
}
34+
35+
return sum;
36+
}

Greedy/DistributeCandy.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
There are N children standing in a line. Each child is assigned a rating value.
3+
4+
You are giving candies to these children subjected to the following requirements:
5+
Each child must have at least one candy.
6+
Children with a higher rating get more candies than their neighbors.
7+
What is the minimum candies you must give?
8+
9+
Sample Input :
10+
11+
Ratings : [1 2]
12+
Sample Output :
13+
14+
3
15+
The candidate with 1 rating gets 1 candy and candidate with rating cannot get 1 candy as 1 is its neighbor. So rating 2 candidate gets 2 candies. In total, 2+1 = 3 candies need to be given out.
16+
17+
LINK: https://www.interviewbit.com/problems/distribute-candy/
18+
*/
19+
20+
int Solution::candy(vector<int> &A)
21+
{
22+
int n = A.size();
23+
24+
vector<int> l(n,1), r(n,1);
25+
26+
for(int i=1;i<n;i++)
27+
if(A[i] > A[i-1])
28+
l[i] = l[i-1] + 1;
29+
30+
for(int i=n-2;i>=0;i--)
31+
if(A[i] > A[i+1])
32+
r[i] = r[i+1] + 1;
33+
34+
int ans = 0;
35+
36+
for(int i=0;i<n;i++)
37+
ans += max(l[i], r[i]);
38+
39+
return ans;
40+
}

Greedy/GasStation.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
3+
4+
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
5+
6+
Return the minimum starting gas station’s index if you can travel around the circuit once, otherwise return -1.
7+
8+
You can only travel in one direction. i to i+1, i+2, ... n-1, 0, 1, 2..
9+
Completing the circuit means starting at i and ending up at i again.
10+
11+
Example :
12+
13+
Input :
14+
Gas : [1, 2]
15+
Cost : [2, 1]
16+
17+
Output : 1
18+
19+
If you start from index 0, you can fill in gas[0] = 1 amount of gas. Now your tank has 1 unit of gas. But you need cost[0] = 2 gas to travel to station 1.
20+
If you start from index 1, you can fill in gas[1] = 2 amount of gas. Now your tank has 2 units of gas. You need cost[1] = 1 gas to get to station 0. So, you travel to station 0 and still have 1 unit of gas left over. You fill in gas[0] = 1 unit of additional gas, making your current gas = 2. It costs you cost[0] = 2 to get to station 1, which you do and complete the circuit.
21+
22+
LINK: https://www.interviewbit.com/problems/gas-station/
23+
*/
24+
25+
int Solution::canCompleteCircuit(const vector<int> &A, const vector<int> &B)
26+
{
27+
int n = A.size();
28+
int gasToReach = 0;
29+
int total = 0;
30+
int start = 0;
31+
32+
for(int i=0;i<n;i++)
33+
{
34+
int gas = (A[i]-B[i]);
35+
total += gas;
36+
37+
if(gasToReach < 0)
38+
{
39+
gasToReach = gas;
40+
start = i;
41+
}
42+
else
43+
gasToReach += gas;
44+
}
45+
46+
if(total < 0)
47+
return -1;
48+
return start;
49+
}

Greedy/HighestProduct.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an array of integers, return the highest product possible by multiplying 3 numbers from the array
3+
4+
Input:
5+
6+
array of integers e.g {1, 2, 3}
7+
NOTE: Solution will fit in a 32-bit signed integer
8+
Example:
9+
10+
[0, -1, 3, 100, 70, 50]
11+
12+
=> 70*50*100 = 350000
13+
14+
LINK: https://www.interviewbit.com/problems/highest-product/
15+
*/
16+
17+
int Solution::maxp3(vector<int> &A)
18+
{
19+
int n = A.size();
20+
21+
sort(A.begin(), A.end());
22+
23+
return max(A[n-1]*A[n-2]*A[n-3], A[n-1]*A[0]*A[1]);
24+
}

Greedy/MajorityElement.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given an array of size n, find the majority element. The majority element is the element that appears more than floor(n/2) times.
3+
4+
You may assume that the array is non-empty and the majority element always exist in the array.
5+
6+
Example :
7+
8+
Input : [2, 1, 2]
9+
Return : 2 which occurs 2 times which is greater than 3/2.
10+
11+
LINK: https://www.interviewbit.com/problems/majority-element/
12+
*/
13+
14+
int Solution::majorityElement(const vector<int> &A)
15+
{
16+
int n = A.size();
17+
int cnt = 1;
18+
int majInd = 0;
19+
20+
for(int i=1;i<n;i++)
21+
{
22+
if(A[majInd]==A[i])
23+
cnt++;
24+
else
25+
cnt--;
26+
27+
if(cnt==0)
28+
{
29+
cnt = 1;
30+
majInd = i;
31+
}
32+
}
33+
34+
return A[majInd];
35+
}

Greedy/Seats.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
There is a row of seats. Assume that it contains N seats adjacent to each other. There is a group of people who are already seated in that row randomly. i.e. some are sitting together & some are scattered.
3+
4+
An occupied seat is marked with a character 'x' and an unoccupied seat is marked with a dot ('.')
5+
6+
Now your target is to make the whole group sit together i.e. next to each other, without having any vacant seat between them in such a way that the total number of hops or jumps to move them should be minimum.
7+
8+
Return minimum value % MOD where MOD = 10000003
9+
10+
Example
11+
12+
Here is the row having 15 seats represented by the String (0, 1, 2, 3, ......... , 14) -
13+
14+
. . . . x . . x x . . . x . .
15+
16+
Now to make them sit together one of approaches is -
17+
. . . . . . x x x x . . . . .
18+
19+
Following are the steps to achieve this -
20+
1 - Move the person sitting at 4th index to 6th index -
21+
Number of jumps by him = (6 - 4) = 2
22+
23+
2 - Bring the person sitting at 12th index to 9th index -
24+
Number of jumps by him = (12 - 9) = 3
25+
26+
So now the total number of jumps made =
27+
( 2 + 3 ) % MOD =
28+
5 which is the minimum possible jumps to make them seat together.
29+
30+
There are also other ways to make them sit together but the number of jumps will exceed 5 and that will not be minimum.
31+
32+
For example bring them all towards the starting of the row i.e. start placing them from index 0.
33+
In that case the total number of jumps will be
34+
( 4 + 6 + 6 + 9 )%MOD
35+
= 25 which is very costly and not an optimized way to do this movement
36+
37+
LINK: https://www.interviewbit.com/problems/seats/
38+
*/
39+
40+
#define MOD 10000003
41+
42+
int Solution::seats(string A)
43+
{
44+
int n = A.size();
45+
vector<int> prsn;
46+
47+
for(int i=0;i<n;i++)
48+
{
49+
if(A[i] == 'x')
50+
prsn.push_back(i);
51+
}
52+
53+
int m = prsn.size();
54+
55+
if(m==0)
56+
return 0;
57+
58+
int median = prsn[m/2];
59+
60+
if(m/2==0)
61+
median = (prsn[m/2-1] + prsn[m/2])/2;
62+
63+
int ans = 0;
64+
65+
int emptyInd = A[median]=='.' ? median : median-1;
66+
67+
for(int i=median-1;i>=0;i--)
68+
{
69+
if(A[i]=='x')
70+
{
71+
ans = (ans + (emptyInd - i))%MOD;
72+
emptyInd--;
73+
}
74+
}
75+
76+
emptyInd = median + 1;
77+
78+
for(int i=median+1;i<n;i++)
79+
{
80+
if(A[i]=='x')
81+
{
82+
ans = (ans + (i - emptyInd))%MOD;
83+
emptyInd++;
84+
}
85+
}
86+
87+
return ans;
88+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy