본문 바로가기

leetcode

(26)
[weekly contest 426] 3371. Identify the Largest Outlier in an Array n개의 원소를 가지는 array가 있는데, n-2 elemnts는 specail number이다. 남은 2개중 하나는 special numbers이고, 다른 하나는 outlier이다.  outlier은  special numbers 중에 하나가 아니며, special numbers의 합이 아닌 수이다. 가장큰 largest potential outlier 을 nums에서 구하라.   원래는 nums에서 2개를 골라 진행하려고 했는데, 그 경우 Time limit이 발생한다. 한번만 돌아야한다.  key idea 1. TL이 발생하지 않으려면, nums에서 for로 1번만 훑으면서 outlier를 찾아야한다. 2.nums에서 1번만 훑을때, outlier 인지 어떻게 알 수 있을까?nums를 1번만 훑었을..
169. Majority Element (top interview 150) 되게 쉬워보이긴 하는데, "  solve the problem in linear time and in O(1) space?" 이걸 고려해서 조금 더 생각해보자.  Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.  Example 1:Input: nums = [3,2,3] Output: 3Example 2:Input: nums = [2,2,1,1,1,2,2] Output: 2  Constraints..
27. Remove Element 한줄요약 nums에 val이 나타는데, in place로 순서를 바꿔라 다음 룰을 따라라. 앞쪽부터 k개의 원소가 val과 같지 않은걸 포함하면 된다.  순서는 상관없고, 포함되기만 하면 된다.  모르겠다. two pointer가 힌트란다.  흠 재는 힌트가 맞다.   two point로 풀면 되는데, i로 배열을 순회하는데, val과 같지 않은 대상을 찾으면 index에 넣고, index를 증가한다.i는 선행하고, index가 후행이 된다.  gpt설명은 언제들어도 좋다.. def removeElement(nums, val):     # 초기화: 인덱스 i는 비대상 값이 저장될 위치를 가리킴     i = 0     # 배열을 순회하며 값을 확인     for j in range(len(nums)): ..
스택/큐? 1. 선입선출! queue ## 선입선출 import queue data = queue.Queue() data.put(2) first = data.get() 2. 후입선출! Stack ( append, pop ) stack = [1,2,3] stack.append(4) last = stack.pop() print(last, stack) # => 4 [1, 2, 3] ## first = stack.pop(0) print(last, stack) # => 1 [2, 3, 4] 3. 양방향큐! deque from collections import deque deq = deque() deq.appendleft(10) # Add first deq.append(0) # Add last deq.popleft() # p..
[python][leetcode 875] Koko Eating Bananas --binary search, 이분탐색 Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours. Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour. K..
[python][leetcode 129]. Sum Root to Leaf Numbers -- dfs,recur You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children. 한줄해석. 좌측과 같은 tree가 있을때, lea..
72. Edit Distance (classic. DP problem) Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: Insert a character Delete a character Replace a character Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') 한줄요약 horse -> ros..
[python][leetcode 2575] Find the Divisibility Array of a String 2575. Find the Divisibility Array of a String You are given a 0-indexed string word of length n consisting of digits, and a positive integer m. The divisibility array div of word is an integer array of length n such that: div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or div[i] = 0 otherwise. Return the divisibility array of word. 한줄요약 : 나눠지면, 1, 안나눠지면 0 넣기 Input: word = "99..