Soluciones:
Los últimos tres me toco con ayuda parcial, pero tremenda la práctica.
Introducción
Arrays y Strings para resolver algoritmos avanzados
Arrays y Strings en detalle
Dos Apuntadores
Patrón de Dos Apuntadores
Verifying Alien Dictionary: análisis del problema
Solución de Verifying Alien Dictionary
Playground: Verifying Alien Dictionary
Programando Verifying Alien Dictionary con JavaScript
Merge Two Sorted Lists: análisis del problema
Solución de Merge Two Sorted Lists
Playground: Merge Two Sorted Lists
Programando Merge Two Sorted Lists con Python
Container With Most Water: análisis del problema
Solución de Container With Most Water
Playground: Container with Most Water
Programando Container With Most Water con Java
Reto: Trapping Rain Water
Ejercicios recomendados de Dos Apuntadores
Ejercicios resueltos de Dos Apuntadores
Ventana Deslizante
Patrón de Ventana Deslizante
Longest Substring Without Repeating Characters: análisis del problema
Solución de Longest Substring Without Repeating Characters
Playground: Longest Substring Without Repeating Characters
Programando Longest Substring Without Repeating Characters con Python
Ejercicios recomendados de Ventana Deslizante
Ejercicios resueltos de Ventana Deslizante
Búsqueda Binaria
Algoritmo de Búsqueda Binaria
Search in Rotated Arrays: análisis del problema
Solución de Search in Rotated Arrays
Playground: Search in Rotated Arrays
Programando Search in Rotated Arrays
Search 2D Array Matrix: análisis del problema
Solución de Search 2D Array Matrix
Playground: Search 2D Array Matrix
Programando Search 2D Array Matrix
Próximos pasos
Toma el Curso Avanzado de Algoritmos: Estructuras de Datos Lineales
Lectura
Ahora vamos a poner en práctica todo lo que hemos aprendido durante este módulo con algunos retos. ¡Empecemos!
Espero tus soluciones en la sección de aportes con tu lenguaje preferido, también puedes ver y aportar a las soluciones de tus compañeras y compañeros. 👨🚀🚀
...
Regístrate o inicia sesión para leer el resto del contenido.
Aportes 22
Preguntas 0
Los últimos tres me toco con ayuda parcial, pero tremenda la práctica.
A continación mi aporte:
const moveZeros = (nums) => {
let len = nums.length;
let pointer = 0;
while(len > 0) {
if(nums[pointer] === 0) {
nums.splice(pointer,1);
nums.push(0);
} else {
pointer++;
}
len--;
}
console.log(nums);
}
moveZeros([0,1,0,3,12]);
moveZeros([0]);
const invertString = (text) => {
let left = 0;
let right = text.length - 1;
while(left < right) {
currentValue = text[right]
text[right] = text[left];
text[left] = currentValue;
left++;
right--;
}
console.log(text);
}
invertString(['h','e','l','l','o']);
invertString(["H", "a", "n", "n", "a", "h"]
);
const sortColors = (nums) => {
let len = nums.length;
let pointer = 0;
while(len > 0) {
if(nums[pointer] === 0) {
nums.splice(pointer, 1);
nums.unshift(0);
} else if(nums[pointer] === 2) {
nums.splice(pointer, 1);
nums.push(2);
}
pointer++;
len--;
}
console.log(nums);
}
sortColors([2,0,2,1,1,0]);
sortColors([2,0,1]);
const invertVowels = (text) => {
const vowels = ['a', 'e', 'i', 'o', 'u'];
const newText = text.split("");
console.log(newText);
let left = 0;
let right = text.length - 1;
let currentChar = '';
while (left < right) {
if (vowels.includes(newText[left]) && vowels.includes(newText[right])) {
currentChar = newText[right];
newText[right] = newText[left];
newText[left] = currentChar;
left++;
right--;
} else if (!vowels.includes(newText[left])) {
left++;
} else if (!vowels.includes(newText[right])) {
right--
}
}
console.log(newText.join(""));
}
invertVowels("hola");
invertVowels("leetcode");
const sortList = (nums) => {
let left = 0;
let right = nums.length - 1;
for (const key in nums) {
nums[key] = Math.pow(Math.abs(nums[key]), 2);
}
while(left < right) {
if(nums[left] > nums[right]) {
nums.splice(right + 1, 0, nums[left]);
nums.splice(left, 1);
} else {
right--;
}
}
console.log(nums);
}
sortList([-4,-1,0,3,10]);
sortList([-7,-3,2,3,11]);
const palindrome = (text) => {
const newText = text.split("");
let left = 0;
let right = newText.length - 1;
let currentChar = '';
while(left < right) {
currentChar = newText[right]
newText[right] = newText[left];
newText[left] = currentChar;
left++;
right--;
}
return newText.join("") === text ? true : false;
}
console.log(palindrome("aba"));
console.log(palindrome("abca"));
const intersectList = (firstList, secondList) => {
let left = 0;
let right = 0;
const newList = [];
const len = Math.min(firstList.length, secondList.length);
while(left < len && right < len) {
const currentList = [];
if(firstList[left][0] <= secondList[right][0] && secondList[right][0] <= firstList[left][1] ) {
currentList.push(secondList[right][0]);
}
if(firstList[left][0] <= secondList[right][1] && secondList[right][1] <= firstList[left][1] ) {
currentList.push(secondList[right][1]);
}
if(secondList[right][0] <= firstList[left][0] && firstList[left][0] <= secondList[right][1] ) {
currentList.push(firstList[left][0]);
}
if(secondList[right][0] <= firstList[left][1] && firstList[left][1] <= secondList[right][1] ) {
currentList.push(firstList[left][1]);
}
if(currentList.length > 0) {
newList.push(currentList);
}
if(firstList[left][0] < secondList[right][0]) {
left++;
} else {
right++;
}
}
console.log(newList);
}
intersectList([[0,2],[5,10],[13,23],[24,25]], [[1,5],[8,12],[15,24],[25,26]]);
intersectList([[1,3],[5,9]], []);
My solution using javascript. I wasn’t able to make it linear in time, I used two nested loops.
<code>var nums = [2, 0, 2, 1, 1, 0];
var left = 0;
var right = 1;
while (left < nums.length) {
right=left+1;
while (right < nums.length) {
if (nums[left] > nums[right]) {
let temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
right++;
}
left++;
}
console.log(nums);
Mi solución para invertir string en Python
<code>
s = ["h","e","l","l","o"]
long = len(s);
i = 0
while i<long//2: # T=O(n) S=O(1)
s[i],s[long-1-i] = s[long-1-i],s[i]
i+=1
print(s)
MOVE ZEROS TO THE END BY ME
import java.util.Arrays;
public class Ceros {
public static void main(String[] args){
// int[] nums= {1,3,0,8,0,9,0,0,0,1,5,2,6,3,0,0,3,0,1,5,9};
int[] nums= {1,3,0,8,0,9,0,0,0,1,5,2,6,3,0,0,3,0,1,5,9,1,2,0,0,0,3,4,5,6,7,0,1};
int p1; int zeros=0;
for(int p2 = nums.length - 1; p2>=0; p2--){
if(nums[p2]!=0){
continue;
}
for(p1 = p2; p1 < nums.length-zeros-1; p1++){
nums[p1]=nums[p1+1];
}
nums[p1]=0;
zeros ++;
// System.out.println(Arrays.toString(nums));
}
System.out.println(Arrays.toString(nums));
}
}
Mi aporte para el ejercicio “Invertir vocales”. Decidí usar un diccionario/objeto para las vocales, ya que la función Array.prototype.includes hace una búsqueda lineal de forma ascendente pasando por todos los elementos del arreglo.
function reverseVowels(string) {
// O(1)
let left = 0;
let right = string.length - 1;
let temp = "";
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
const dictionary = {};
string = Array.from(string);
// Building the vowels dictionary. O(n)
vowels.forEach(character => {
dictionary[character] = character; // O(1)
});
// O(n)
while (left < right) {
// Pointer have found a vowel
if (dictionary[string[left]] && dictionary[string[right]]) {
temp = string[left];
string[left] = string[right];
string[right] = temp;
left++;
right--;
}
if (!dictionary[string[left]]) {
// No vowel on left, increase pointer.
left++;
} else if (!dictionary[string[right]]) {
// No vowel on right, decrease pointer.
right--;
}
}
return string.join("");
}
Mi aporte para el ejercicio “Invertir string”
function reverseList(list) {
let left = 0;
let right = list.length - 1;
let temp = ""
while (left < right) {
temp = list[right];
list[right] = list[left];
list[left] = temp;
left++;
right--;
}
return list;
}
Mi aporte para el ejercicio “Mover ceros al final”:
function rearrangeList(numbers) {
let left = 0;
let right = 1;
while (right < numbers.length) {
if (numbers[left] === 0 && numbers[right] != 0) {
numbers[left] = numbers[right];
numbers[right] = 0;
left++;
right++;
} else if (numbers[left] === 0 && numbers[right] === 0) {
right++;
}
}
return numbers;
}
Mi aporte para el 2 ejemplo:
public static void InvertString(char[] text)
{
int left = 0;
int right = text.Length - 1;
while (left < right)
{
var leftVal = text[left];
var rightVal = text[right];
text[left] = rightVal;
text[right] = leftVal;
left++;
right--;
}
}
Adjunto mi posible solucion para el ejemplo 1
public static void MoveZerosFinal(int[] numbers)
{
int arrayLength = numbers.Length;
int startPointer = 0;
int endPointer = numbers.Length - 1;
while (startPointer < endPointer)
{
var currentStartPointerValue = numbers[startPointer];
var currenteEndPointerValue = numbers[endPointer];
if (currentStartPointerValue == 0 && currenteEndPointerValue != 0)
{
if (numbers[startPointer + 1] < currenteEndPointerValue)
{
var nextStartPointerValue = numbers[startPointer + 1];
numbers[startPointer] = nextStartPointerValue;
numbers[startPointer + 1] = currenteEndPointerValue;
numbers[endPointer] = currentStartPointerValue;
startPointer++;
endPointer--;
}
else
{
var previousStartPointerValue = numbers[startPointer - 1];
numbers[startPointer] = previousStartPointerValue;
numbers[startPointer - 1] = currenteEndPointerValue;
numbers[endPointer] = currentStartPointerValue;
startPointer++;
endPointer--;
}
}
else
{
startPointer++;
}
}
}
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?