new
BAN USER
- 0of 0 votes
AnswersN different couple go to cinema with 2N different seats. They take their place randomly. You could make swap operations. Write a code for given input what is the minimum number of swap operations for sitting all couples with their partners? Additionally, be sure that no one swaps more than 2 times.
- new in United States| Report Duplicate | Flag | PURGE
Google SDE1 Algorithm Arrays Coding Data Structures - 2of 2 votes
AnswersWe define the following:
- new in United States
There are friends_nodes friends numbered from 1 to friends_nodes.
There are friends_edges pairs of friends, where each (xi, yi) pair of friends is connected by a shared integer token described by friends_weighti.
Any two friends, xi and yi, can be connected by zero or more tokens because if friends xi and yi share token ti and friends yi and zi also share token ti, then xi and zi are also said to share token ti.
Find the maximal product of xi and yi for any directly or indirectly connected (xi, yi) pair of friends such that xi and yi share the maximal number of tokens with each other.
Complete the maxTokens function in the editor. It has four parameters:
Name Type Description
friends_nodes integer The number of friends.
friends_from integer array Each friends_from[i] (where 0 ≤ i < friends_edges) denotes the first friend in pair (friends_from[i], friends_to[i]).
friends_to integer array Each friends_to[i] (where 0 ≤ i < friends_edges) denotes the second friend in pair (friends_from[i], friends_to[i]).
friends_weight integer array Each friends_weight[i] (where 0 ≤ i < friends_edges) denotes the ID number of a token shared by both friends_from[i] and friends_to[i].
Note: friends_edges is the number of pairs of friends that directly share a token.
The function must return an integer denoting the maximal product of xi and yi such that xi and yi are a pair of friends that share the maximal number of tokens with each other.
Input Format
The first line contains two space-separated integers describing the respective values of friends_nodes and friends_edges.
Each line i of the friends_edges subsequent lines (where 0 ≤ i < friends_edges) contains three space-separated integers describing the respective values of friends_fromi, friends_toi, and friends_weighti.
Constraints
2 ≤ friends_nodes ≤ 100
1 ≤ friends_edges ≤ min(200, (friends_nodes × (friends_nodes − 1)) / 2)
1 ≤ friends_weighti ≤ 100
1 ≤ friends_fromi, friends_toi ≤ friends_nodes
1≤ friends_weighti ≤ friends_edges
friends_fromi ≠ friends_toi
Each pair of friends can be connected by zero or more types of tokens.
Output Format
Return an integer denoting the maximal product of xi and yi such that xi and yi are a pair of friends that share the maximal number of tokens with each other.
Sample Input 0
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
Sample Output 0
6
Explanation 0
Each pair of n = 4 friends is connected by the following tokens:
Pair (1, 2) shares 2 tokens (i.e., tokens 1 and 2)
Pair (1, 3) shares 1 token (i.e., token 1)
Pair (1, 4) shares 0 tokens
Pair (2, 3) shares 2 tokens (i.e., tokens 1 and 3)
Pair (2, 4) shares 1 token (i.e., token 3)
Pair (3, 4) shares 1 token (i.e., token 3)
The pairs connected by the maximal number of tokens are (1, 2) and (2, 3). Their respective products are 1 × 2 = 2 and 2 × 3 = 6. We then return the largest of these values as our answer, which is 6.| Report Duplicate | Flag | PURGE
Google - 1of 1 vote
AnswersSuperCity consists of a single line of n buildings, where each building i is heighti units tall; however, SuperCity is under attack by two supervillains looking to show off their superpowers! The supervillains are standing on opposite ends of the city, unleashing their powers in an attempt to destroy all n buildings. In a single move, a supervillain unleashes their power and destroys the nearest contiguous block of buildings in which the respective heights of each building are nondecreasing. In other words:
- new in United States
If a supervillain is standing on the left end of the city and the nearest intact building is building i, then performing a move will destroy each consecutive building i, i + 1, i + 2, … satisfying heighti ≤ heighti+1 ≤ heighti+2 ≤ … until there are either no more buildings in their path or there is some building j satisfying heightj > heightj+1.
If a supervillain is standing on the right end of the city and the nearest intact building is building i, then performing a move will destroy each consecutive building i, i − 1, i − 2, … satisfying heighti ≤ heighti-1 ≤ heighti-2 ≤ … until there are either no more buildings in their path or there is some building j satisfying heightj > heightj-1.
Once a supervillain destroys a building, the building's height becomes 0.
Complete the function in the editor below. It has one parameter: an array of integers, height, where each heighti denotes the height of building i. The function must return an integer denoting the minimum number of total moves needed for two supervillains standing on opposite ends of the city (as described by the array of building heights) to destroy all n buildings.
Input Format
The first line contains an integer, n, denoting the number of elements in height.
Each line i of the n subsequent lines contains an integer describing heighti.
Constraints
1≤ n ≤ 105
1 ≤ heighti ≤ 105, where 0 ≤ i < n.
Output Format
Return an integer denoting the minimum number of total moves needed for two supervillains on opposite ends of the array to destroy all n buildings.
Sample Input 0
8
1
2
3
4
8
7
6
5
Sample Output 0
2
Explanation 0
The respective heights of each building are given as height = [1, 2, 3, 4, 8, 7, 6, 5]. The supervillains can perform the following minimal sequence of moves:
Sample Case 0
The diagram above depicts the changes to SuperCity's skyline after each move by a supervillain.
In the first move, the supervillain on the left destroys buildings 0 through 4, because height0 ≤ height1 ≤ height2 ≤ height3 ≤ height4; note that the destruction stops at this point, as height4 > height5.
In the second move, the supervillain on the right destroys buildings 7 through 5, because height7 ≤ height6 ≤ height5.
As it took a minimal two moves for the supervillains to level all the buildings, the function returns 2.
Sample Input 1
6
1
2
1
2
10
9
Sample Output 1
3
Explanation 1
The respective heights of each building are given as height = [1, 2, 1, 2, 10, 9]. The supervillains can perform the following minimal sequence of moves:
Sample Case 1
The diagram above depicts the changes to SuperCity's skyline after each move by a supervillain.
In the first move, the supervillain on the left destroys buildings 0 through 1, because height0 ≤ height1.
In the second move, the supervillain on the right destroys buildings 5 through 4, because height5 ≤ height4.
In the third move, the supervillain on the left destroys buildings 2 through 3, because height2 ≤ height3.
As it took a minimal three moves for the supervillains to level all the buildings, the function returns 3.| Report Duplicate | Flag | PURGE
Google Algorithm
/*
* Complete the function below.
*/
static int getMinimumMoves(int[] height) {
int[][] dp = new int[height.length][height.length];
return getMinimumNumberOfMoves(0,height.length-1, height,dp);
}
static int getMinimumNumberOfMoves(int start, int end, int[] a,int[][] dp){
if(start>end) return 0;
if(start == end) return 1;
if(dp[start][end] != 0) return dp[start][end];
int count = 0;
int i=start;
while(i<end && a[i]<=a[i+1])
i++;
int j = end;
while(j>start && a[j]<=a[j-1])
j--;
int count1 = 1+ getMinimumNumberOfMoves(i+1, end, a,dp);
int count2 = 1+ getMinimumNumberOfMoves(start,j-1, a,dp);
// System.out.println(start + " "+ end +" " +count1 + " " + count2);
dp[start][end] = Math.min(count1, count2);
return dp[start][end];
}
import java.util.HashMap;
import java.util.Map;
public class QuicklyFindingSumOFTwoNumber {
public static void main(String[] args) {
Map<Integer ,Integer> valueMap = new HashMap<Integer ,Integer>();
int a[] = { 0,10,2,3,4,5,6,7,8,9,0,123,34};
for(int i=0;i<a.length;i++){
if(a[i]<=10){
int temp = 10-a[i];
if(valueMap.containsKey(temp)){
System.out.println("positions :("+valueMap.get(temp) +" " + i +")" + temp +" " + a[i]);
System.exit(0);
}
valueMap.put(a[i], i);
}
}
}
}
Initial we can start with size of the array as 10 or more than that.
If it reaches boundary we can double it or we can increase by 1.5 times.
we have to copy all the elements from the old array to new array.
We can maintain a variable which contains the number of elements presently array holding
RepEarned praise for analyzing acne for the government. Earned praised for my work implementing mantra to get desired husband in ...
RepDo you need dua for controlling husband? Contact Guru ji right now. He provides the best and simple dua to ...
RepA real dynamo when it comes to buying and selling carnival rides in Fort Lauderdale, FL. Spend several years working ...
Repmariacbrister, Graphics Programmer at Graphic Systems
Hey, I am Maria and I am from Bluefield. Currently, I work as a freelancer graphic artist. From an early ...
RepHad moderate success buying and selling weebles in Ohio. Had some great experience buying and selling wooden trains on Wall ...
RepTop Tiers provides latest english premier league articles and news to keep you up to date. Visit our website today ...
Repstacimdalton, Dev Lead at ASAPInfosystemsPvtLtd
At the moment I'm implementing Slinkies in the financial sector. My current pet project is researching break up a ...
RepSpent 2001-2004 selling UFOs for the government. Have some experience with Internet Marketing Services New York. Set new standards for ...
RepBlakeRileyHomes, Analyst at Aristocrat Gaming
Blake Riley Homes is the leading interior designing and real estate home staging Orange County CA company.Our professional designs ...
RepLeonaDWilliams, Analyst at CCN
At the moment I'm building banjos in Deltona, FL. Once had a dream of analyzing easy-bake-ovens in Fort Walton ...
RepHave some experience building shaving cream in Mexico. Prior to my current job I was marketing inflatable dolls in Jacksonville ...
RepDo you want to marry with your desired person? The Islamic dua for marriage with a loved one has great ...
RepDaniel Wattss, Cloud Support Associate at A9
Spent 2001-2007 getting to know pogo sticks for no pay. Enthusiastic about researching deodorant in Los Angeles, CA. Prior to ...
@stupid.innovates
- new September 22, 2017[1,1,1,1,1] -> can you handle the case where the building height are equal.
Nice solution