Sunteți pe pagina 1din 12

Ex.

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing
sequence by removing no more than one element from the array.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence
containing only one element is also considered to be strictly increasing.
Example
 For sequence = [1, 3, 2, 1] , the output should be
almostIncreasingSequence(sequence) = false .

There is no one element in this array that can be removed in order to get a strictly increasing
sequence.

 For sequence = [1, 3, 2] , the output should be


almostIncreasingSequence(sequence) = true .
You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately,
you can remove 2 to get the strictly increasing sequence [1, 3].
Input/Output
 [execution time limit] 4 seconds (js)

 [input] array.integer sequence

Guaranteed constraints:
2 ≤ sequence.length ≤ 10 5,
-105 ≤ sequence[i] ≤ 10 5.

 [output] boolean

o Return true if it is possible to remove one element from the array in order to get a
strictly increasing sequence, otherwise return false.

function almostIncreasingSequence(sequence) {

var greseli = 0;

var cur = 0;

var next = 1;

if(sequence.length == 1)

return true;

while (next < sequence.length) {


if(sequence[cur] >= sequence[next])

greseli++;

if(next+1 < sequence.length && sequence[cur] > sequence[next+1])

if(sequence[cur-1] >= sequence[next])

greseli++;

cur = next;

next++;

else

cur = next;

next++;

return greseli <= 1;

Rezolvare:

function almostIncreasingSequence(sequence) {

let invalidItemsCount = 0;

for (let i = 1; i < sequence.length; i++) {

if (sequence[i] <= sequence[i-1]) {

invalidItemsCount++;

if (invalidItemsCount > 1) return false;


if (sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]) return false;

return true;

Ex. 8

After becoming famous, the CodeBots decided to move into a new building together. Each of the
rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are
haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free r ooms, or
any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your
task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all th e values
that don't appear below a 0).
Example

 For

 matrix = [[0, 1, 1, 2],


 [0, 5, 0, 0],
 [2, 0, 3, 3]]
the output should be
matrixElementsSum(matrix) = 9 .
 There are several haunted rooms, so we'll disregard them as well as any rooms beneath them.
Thus, the answer is 1 + 5 + 1 + 2 = 9.

 For

 matrix = [[1, 1, 1, 0],


 [0, 5, 0, 1],
 [2, 1, 3, 10]]
the output should be
matrixElementsSum(matrix) = 9 .
 Note that the free room in the final column makes the full column unsuitable for bots (not just
the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9 .
Input/Output
 [execution time limit] 4 seconds (js)

 [input] array.array.integer matrix

A 2-dimensional array of integers representing the cost of each room in the building. A value
of 0 indicates that the room is haunted.
 Guaranteed constraints:
1 ≤ matrix.length ≤ 5 ,
1 ≤ matrix[i].length ≤ 5 ,
0 ≤ matrix[i][j] ≤ 10 .

 [output] integer

o The total price of all the rooms that are suitable for the CodeBots to live in.

Rezolvare:

function matrixElementsSum(matrix) {

var priceTotal = 0;

var index = [];

for (var i=0; i<matrix.length;i++)

for (var j=0; j<matrix[i].length;j++)

if (matrix[i][j]==0)

index.push(j);

else if (index.indexOf(j)==-1)

priceTotal=priceTotal+matrix[i][j];

return priceTotal;

}
Ex. 9:

Given an array of strings, return another array containing all of its longest strings.

Example
For inputArray = ["aba", "aa", "ad", "vcd", "aba"] , the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"] .
Input/Output
 [execution time limit] 4 seconds (js)

 [input] array.string inputArray

A non-empty array.

Guaranteed constraints:
1 ≤ inputArray.length ≤ 10 ,
1 ≤ inputArray[i].length ≤ 10 .

 [output] array.string

o Array of the longest strings, stored in the same order as in the inputArray.

Rezolvare:

function allLongestStrings(inputArray) {

return inputArray.filter(str => str.length === inputArray.map(str => str.length).sort((a, b) => a -


b).pop());

Sau

https://www.youtube.com/watch?v=5aO-EunylgQ:

function allLongestStrings(inputArray) {

var longestStrings = inputArray[0].length;

for (var i=1; i<inputArray.length;i++) {

if (longestStrings < inputArray[i].length){

longestStrings = inputArray[i].length;

inputArray = inputArray.filter((element)=> {

return element.length === longestStrings;

});
return inputArray;

Ex. 10:

Given two strings, find the number of common characters between them.

Example
For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3 .
Strings have 3 common characters - 2 "a"s and 1 "c".
Input/Output
 [execution time limit] 4 seconds (js)

 [input] string s1

A string consisting of lowercase English letters.

Guaranteed constraints:
1 ≤ s1.length < 15 .

 [input] string s2

 A string consisting of lowercase English letters.

Guaranteed constraints:
1 ≤ s2.length < 15 .

 [output] integer

Rezolvare:

https://www.youtube.com/watch?v=EMjv-VKAnEE

function commonCharacterCount(s1, s2) {

//impartim cuvintele in litere si le ducem intr-un vector

s1 = s1.split("");

s2 = s2.split("");

var s1Object = {};

var s2Object = {};


for (var i = 0; i< s1.length; i++) {

if (s1Object.hasOwnProperty(s1[i]) === false) {

s1Object[s1[i]] = 1;

else {

s1Object[s1[i]]++;

for (var i = 0; i< s2.length; i++) {

if (s2Object.hasOwnProperty(s2[i]) === false) {

s2Object[s2[i]] = 1;

else {

s2Object[s2[i]]++;

var total = 0;

for (var prop in s1Object) {

if (s2Object.hasOwnProperty(prop) === true) {

if (s2Object[prop] < s1Object[prop]) {

total = total + s2Object[prop];

else {

total = total + s1Object[prop];

return total;
}

Ex. 12:

Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the
sum of the first half of the digits is equal to the sum of the second half.

Given a ticket number n, determine if it's lucky or not.


Example
 For n = 1230, the output should be
isLucky(n) = true ;
 For n = 239017, the output should be
isLucky(n) = false .

Input/Output
 [execution time limit] 4 seconds (js)

 [input] integer n

A ticket number represented as a positive integer with an even number of digits.

Guaranteed constraints:
10 ≤ n < 106.

 [output] boolean

o true if n is a lucky ticket number, false otherwise.

Rezolvare:

https://www.youtube.com/watch?v=e45wqk1zUgE

function isLucky(n) {

var num = n.toString();

var half = num.length/2;

var firstHalfSum = 0;

var secondHalfSum = 0;

for (var i = 0; i < half; i++) {

firstHalfSum += parseInt(num[i]);

}
for (var j = half; j < num.length; j++) {

secondHalfSum += parseInt(num[j]);

if (firstHalfSum === secondHalfSum) {

return true;

return false;

Ex 12

Some people are standing in a row in a park. There are trees between them which cannot be moved.
Your task is to rearrange the people by their heights in a non-descending order without moving the
trees. People can be very tall!

Example
For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be
sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].
Input/Output
 [execution time limit] 4 seconds (js)

 [input] array.integer a

If a[i] = -1, then the ith position is occupied by a tree. Otherwise a[i] is the height of a
person standing in the ith position.
Guaranteed constraints:
1 ≤ a.length ≤ 1000 ,
-1 ≤ a[i] ≤ 1000 .

 [output] array.integer

o Sorted array a with all the trees untouched.

Rezolvare:

https://www.youtube.com/watch?v=LHn8WEj3CfU

function sortByHeight(a) {

var array2 = a;

array2 = array2.filter((element)=> {
if (element != -1) {

return element;

}).sort((a,b)=> {

return a-b;

});

var index = 0;

for (var i = 0; i< a.length; i++) {

if (a[i] != -1) {

a[i] = array2[index];

index++;

return a;

Ex. 13

Write a function that reverses characters in (possibly nested) parentheses in the input string.

Input strings will always be well-formed with matching ()s.


Example
 For inputString = "(bar)" , the output should be
reverseInParentheses(inputString) = "rab" ;
 For inputString = "foo(bar)baz" , the output should be
reverseInParentheses(inputString) = "foorabbaz" ;
 For inputString = "foo(bar)baz(blim)" , the output should be
reverseInParentheses(inputString) = "foorabbazmilb" ;
 For inputString = "foo(bar(baz))blim" , the output should be
reverseInParentheses(inputString) = "foobazrabblim" .
Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim".

Input/Output
 [execution time limit] 4 seconds (js)

 [input] string inputString


A string consisting of lowercase English letters and the characters ( and ). It is guaranteed that
all parentheses in inputString form a regular bracket sequence.
Guaranteed constraints:
0 ≤ inputString.length ≤ 50 .

 [output] string

o Return inputString, with all the characters that were in parentheses reversed.

Rezolvare

https://www.youtube.com/watch?v=P9osBWtIGp8:

function reverseInParentheses(inputString) {

const re = /\(([^\(\)]*)\)/g;

const flip = (match, capture) => capture.split("").reverse().join("");

while (re.test(inputString)) inputString = inputString.replace(re, flip);

return inputString;

https://www.youtube.com/watch?v=IHGM97lvRGk

S-ar putea să vă placă și