You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Example 1:
Example 2:
/**
* @param {string[][]} paths
* @return {string}
*/
var destCity = function(paths) {
const graph = [];
const citySet = new Set();
paths.forEach(([start, end]) => {
citySet.add(start);
citySet.add(end);
if (graph[start]) {
graph[start].push(end);
} else {
graph[start] = [end];
}
});
const startCityArr = [...Object.keys(graph)];
for (const city of citySet.values()) {
if (!startCityArr.includes(city)) {
return city;
}
}
};
/**
* @param {string[][]} paths
* @return {string}
*/
var destCity = function(paths) {
const startCities = new Set();
const endCities = new Set();
paths.forEach(([start, end]) => {
startCities.add(start);
endCities.add(end);
});
endCities.forEach((endCity) => {
if (startCities.has(endCity)) {
endCities.delete(endCity);
}
});
return endCities.values().next().value;
};
You are given an integer array prices where prices[i] is the price of the ith item in a shop.
There is a special discount for items in the shop. If you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will not receive any discount at all.
Return an integer array answer where answer[i] is the final price you will pay for the ith item of the shop, considering the special discount.
Example 1:
Example 2:
prices.length <= 500prices[i] <= 1000/**
* @param {number[]} prices
* @return {number[]}
*/
var finalPrices = function(prices) {
return prices.map((price, index) => {
let discountedPrice = 0;
for (let i = index + 1; i < prices.length; i++) {
const nextPrice = prices[i];
if (price >= nextPrice) {
discountedPrice = nextPrice;
break;
}
}
return discountedPrice ? price - discountedPrice : price;
});
};
A teacher is writing a test with ntrue/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).
You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:
'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F').Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.
Example 1:
Example 2:
n == answerKey.length1 <= n <= 5 * 10^4answerKey[i] is either 'T' or 'F'1 <= k <= n/**
* @param {string} answerKey
* @param {number} k
* @return {number}
*/
var maxConsecutiveAnswers = function(answerKey, k) {
let maxLength = 0;
let left = 0;
let right = answerKey.length;
let TCount = 0;
let FCount = 0;
let start = 0;
while (left < right) {
if (answerKey[left] === 'T') {
TCount++;
} else {
FCount++;
}
if (TCount <= k || FCount <= k) {
maxLength = Math.max(maxLength, TCount + FCount);
}
while (TCount > k && FCount > k) {
if (answerKey[start] === 'T') {
TCount--;
} else {
FCount--;
}
start++;
}
left++;
}
return maxLength;
};
You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.
Return the minimum number of steps to make t an anagram of s.
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example 1:
Example 2:
1 <= s.length <= 5 * 10^4s.length == t.lengths and t consist of lowercase English letters only./**
* @param {string} s
* @param {string} t
* @return {number}
*/
var minSteps = function(s, t) {
const strSMap = new Map();
let answer = 0;
for (let i = 0; i < s.length; i++) {
const str = s[i];
strSMap.set(str, (strSMap.get(str) || 0) + 1);
}
for (let j = 0; j < t.length; j++) {
const str = t[j];
const strCount = strSMap.get(str);
if (strCount) {
strSMap.set(str, strCount - 1);
}
}
strSMap.forEach((value) => answer += value);
return answer;
};