알고리즘 15

2024 11 09 3133. Minimum Array End

문제You are given two integers n and x. You have to construct an array of positive integers nums of size n where for every 0 요약배열 nums[n-1]을 만들되 모든 수가 AND연산을 하여 x를 만들어야하고 증가 수열이어야한다.풀이일단 전탐은 절대 불가....long범위임;;;그래서 일단 AND의 특성을 활용둘다 1이어야 1이나오는 AND연산이므로x의 이진수 자리에서 0에 해당하는 자리에 n -1 의 이진수를 체워넣으면 된다 (첫번째는 x와 같으므로)풀이 1처음 사용한 방법은 Stack을 사용하였다.n과 x를 2로 나누면서 큰수부터 stack넣었고 차례대로 빼면서 넣었다.class Solution { pu..

알고리즘 2024.11.09

2024 11 06 3011. Find if Array Can Be Sorted

문제You are given a 0-indexed array of positive integers nums.In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).Return true if you can sort the array, else return false.요약10진수의 정수 배열에 대해서 인접한 두 수가 2진법으로 표현시 1의 개수가 같으면 둘의 자리를 교환할 수 있다.이때 이 배열을 정렬할 수 있는가?풀이새로운 배열에 인접하면서 2진법 표현시 ..

알고리즘 2024.11.06

2024 11 05 2914. Minimum Number of Changes to Make Binary String Beautiful

문제You are given a 0-indexed binary string s having an even length.A string is beautiful if it's possible to partition it into one or more substrings such that:Each substring has an even length.Each substring contains only 1's or only 0's.You can change any character in s to 0 or 1.Return the minimum number of changes required to make the string s beautiful.요약0과 1로 이루어진 짝수 문자열에서 짝수길이의 동일한 문자만 가진 ..

알고리즘 2024.11.05

2024 11 04 3163. String Compression III

문제Given a string word, compress it using the following algorithm:Begin with an empty string comp. While word is not empty, use the following operation:Remove a maximum length prefix of word made of a single character c repeating at most 9 times.Append the length of the prefix followed by c to comp.Return the string comp.요약연속된 문자가 몇개있는지 세되 9개 씩만풀이어렵진않다 우선 첫글자를 저장해두고 다음 문자가 같은지 비교후 다르거나 9를 넘는다면 sb..

알고리즘 2024.11.04

2024 11 03 Result796. Rotate String

문제Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.A shift on s consists of moving the leftmost character of s to the rightmost position.For example, if s = "abcde", then it will be "bcdea" after one shift.요약첫글자를 마지막으로 돌리면서 goal과 같은게 나오는지 검사풀이처음에는 String.substring으로 풀긴했는데 효율이 너무 안좋게 나오길레그냥 s 문자 안바꾸고 idx로 검하였다.코드class Solution { public..

알고리즘 2024.11.03

2024 11 04 1957. Delete Characters to Make Fancy String

문제A fancy string is a string where no three consecutive characters are equal.Given a string s, delete the minimum possible number of characters from s to make it fancy.Return the final string after the deletion. It can be shown that the answer will always be unique.요약String에서 연속으로 같은 char 3개가 나오지않게 제거풀이char를 하나씩 조회하면서 counting 하며 3개 이상이 되면 무시, 다른 글자가 나올 경우 count를 초기화하고 진행코드class Solution { pu..

알고리즘 2024.11.01

2024 10 30 1671. Minimum Number of Removals to Make Mountain Array

문제You may recall that an array arr is a mountain array if and only if:arr.length >= 3There exists some index i (0-indexed) with 0 arr[i + 1] > ... > arr[arr.length - 1]Given an integer array nums​​​, return the minimum number of elements to remove to make nums​​​ a mountain array.요약산 모양으로 왼쪽으로는 감소 오른쪽으로는 증가하는 최장 집합를 찾으면 된다.풀이dfs로 깊게 들어가면서 각 index (idx)에서 1. 이전 0~idx 까지 가장 긴 증가 부분집합의 길이2. 이후 idx..

알고리즘 2024.10.30