You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).Find two lines that together with the x-axis form a container, such that the container contains the most water.Return the maximum amound of water a container can store.Notice that you may not slant the container.그림을 보면 쉽게 이해가 가능하다. slant (기..
Given the head of a singly linked list, reverse the list, and return the reversed list.singly linked list 가 주어지면, reversed list를 반환하는 문제다. ListNode 클래스파이썬에서 연결리스트를 구현하기 위해서는 직접 클래스를 정의해야 한다.LeetCode 문제에서 제공되는 연결 리스트 정의는 다음과 같다.# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val # 현재 노드가 저장하는 값# self.next = next # 다..
You are given an array prices where prices[i] is the price of a given stock on the ith day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.주식 가격 배열이 주어졌을 때, 얻을 수 있는 maximum profit을 리턴하는 문제. 풀이1: 브루트포스?..
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.간단한 바이너리서치(이진탐색) 문제!이진탐색은 정렬된 배열에서만 사용할 수 있는 탐색 알고리즘이다. 이진탐색은 매번 탐색 범위를 절반으로 줄이므로 시간복잡도가 O(n)이다.또한 문제에서 명시적으로 O(log n) 시간 복잡도로 해결하라고 요구하고 있는데..
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.주어진 온도 배열에서, 각 날 이후 더 따뜻한 날이 오기까지 며칠을 기다려야 하는지 계산하는 문제이다. 풀이1: 브루트 포스class Solution: def dailyTemperatures(s..
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.Example 1:Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] n 이라는 limit이 있으면,여는 괄호 수를 openN, 닫는 괄호 수를 closeN이라고 했을 때closeN closeN > openN이 되면 well-formed parentheses가 되지 못하기 때문이다. 아래와 같이 트리 구조를 그려보며 가능한 경우의 수를 생각해볼 수 있다. 풀이1: 백트래킹, stack 이용class Solution: def generateParen..
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.Notice that the solution set must not contain duplicate triplets. 풀이 1: 브루트 포스 - 시간 초과class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res = set() nums.sort() for i in range(len(nums)): ..
Postfix NotationYou are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.Evaluate the expression. Return an integer that represents the value of the expression.Note that:The valid operators are '+', '-', '*', and '/'.Each operand may be an integer or another expression.The division between two integers always truncates toward zero.There will ..
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.The tests are generated such that there is exactl..
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.Implement the MinStack class:- MinStack() initializes the stack object.- void push(int val) pushes the element val onto the stack.- void pop() removes the element on the top of the stack.- int top() gets the top element of the stack.- int getMin() retrieves the minimum element in the stack.You must i..