array 3

[알고리즘] Leetcode. Replace Elements with Greatest Element on Right Side

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array. 배열 arr가 주어지고 모든 엘리먼트를 오른쪽에 있는 엘리먼트 중 제일 큰 엘리먼트로 바꾸세요. 그리고 마지막 엘리먼트는 -1로 바꾸세요. 마지막으로 배열을 리턴하세요. - 처음 푼 코드 var replaceElements = function(arr) { let greatestNumber; for (let i = 0; i < arr.length; i++) { greatestN..

[알고리즘] Leetcode. Find Numbers with Even Number of Digits

Given an array nums of integers, return how many of them contain an even number of digits. 정수가 담겨있는 배열이 주어지고, 짝수 자릿수가 몇 개인지 리턴하세요. Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). 6 contains 1 digit (odd number of digits). 7896 contains 4 digits ..

[알고리즘] Leetcode 704. Binary Search 이진 검색 알고리즘

704. Binary Search Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. 오름차순으로 주어진 정수 배열과 정수 타겟이 주어지고 타겟을 숫자로 검색하는 함수를 만드세요. 타겟이 존재한다면 index를 리턴하고 그렇지 않으면 -1를 리턴하세요. 시간 복잡도는 반드시 O(log n)이어야 합니..