전체 글(334)
-
[Leetcode]11. Container With Most Water
1. problem : https://leetcode.com/problems/container-with-most-water/?envType=study-plan-v2&envId=leetcode-75 2. solution 1 :class Solution { public int maxArea(int[] height) { int n = height.length; int i = 0; int j = n - 1; int maxWater = 0; while (i 이 문제는 큰 스틱은 그대로 두고, 작은 스틱을 옮기면서 최댓값은 갱신하는 문제다. 사실, 발상자체가 이해가 되지 않는다. 왜? 작은 스틱을 옮겨야 하는가? 이게 모든 경우에 적용할 수 있는가..
2024.07.29 -
[Leetcode]654. Maximum Binary Tree
1. problem :https://leetcode.com/problems/maximum-binary-tree/ 2. solution 1 : class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length == 0) { return null; } int maxNumIndex = 0; for (int i = 1; i recursion을 이용한 풀이다. 사실 제일 간단한 풀이가 아닐까 싶다. 3. solution 2 :import java.util.Stack;public class Solution { public TreeNode c..
2024.07.29 -
[Leetcode]792. Number of Matching Subsequences(x)
1. problem : https://leetcode.com/problems/number-of-matching-subsequences/ 2. solution 1 : from collections import defaultdictfrom typing import List, Iteratorclass Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: # defaultdict 초기화: 각 문자가 현재 기다리는 단어들을 저장 waiting = defaultdict(list) # 각 단어의 첫 문자를 키로 하고, 나머지 문자의 이터레이터를 값으로 설정 for word ..
2024.07.29 -
[Leetcode]392. Is Subsequence(x)
1. problem : https://leetcode.com/problems/is-subsequence/description/?envType=study-plan-v2&envId=leetcode-75 2. solution 1 :class Solution: def isSubsequence(self, s: str, t: str) -> bool: i,j = 0,0 s_len,t_len = len(s) , len(t) while i 이 solution은 chat gpt 4o가 짜준 코드이다. two-pointer 접근법이다. i는 s를 순회하고, j는 t를 순회한다. s [i]와 s [j]가 같다면, i +=1을 하고, j +=1을 한다. 만약에 다르다면 s의 i는..
2024.07.28 -
[BOJ]10808(O)
1. problem : https://www.acmicpc.net/problem/10808 2. solution 1 : import string n = input()alphabet = string.ascii_lowercasealphabet_dic = {letter:0 for letter in (alphabet)}for char in n: alphabet_dic[char] += 1 print(" ".join(str(alphabet_dic[letter]) for letter in alphabet))string을 import 해서 alphabet을 받는다. 그런 다음 딕셔너리를 만들고 , input의 각각의 알파벳들을 count 한 다음, value값을 출력한다. 3. solution 2 :from ..
2024.07.28 -
[Leetcode]283. Move Zeroes(n^2말고 다른풀이로 풀어볼것)
1. problem :https://leetcode.com/problems/move-zeroes/?envType=study-plan-v2&envId=leetcode-75 2. solution 1 :class Solution: def moveZeroes(self, nums: List[int]) -> None: zero_count = nums.count(0) i = 0 while zero_count > 0: if nums[i] == 0: nums.pop(i) nums.append(0) zero_count -= 1 else: ..
2024.07.28