본문 바로가기

DEVELOP/Algorithm

(4)
[leetcode] 1748. Sum of Unique Elements Q. You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array. Return the sum of all the unique elements of nums. let sumOfUnique = function(nums) { let sum = 0; nums.forEach(function(idx){ if(nums.indexOf(idx)===nums.lastIndexOf(idx)){ sum += idx; } }); return sum; }; 중복되는 요소들을 제외한 수들만 더한 결과를 리턴해야 하므로 indexOf와 lastIndexOf를 이용하여, 두..
[leetcode] 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence Q. Given a sentence that consists of some words separated by a single space, and a searchWord. You have to check if searchWord is a prefix of any word in sentence. Return the index of the word in sentence where searchWord is a prefix of this word (1-indexed). If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1. A..
[leetcode] 1720. Decode XORed Array Q. There is a hidden integer array arr that consists of n non-negative integers. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = arr[i] XOR arr[i + 1]. For example, if arr = [1,0,2,1], then encoded = [1,2,3]. You are given the encoded array. You are also given an integer first, that is the first element of arr, i.e. arr[0]. Return the original array arr...
[leetcode] 1431. Kids With the Greatest Number of Candies Q. Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has. For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies. let kidsWithCandies = function(candies, extraCandie..