Hyoseo Lee
Home About Projects Blog Thinking
leetcode —

Archive/LeetCode

Problem solutions — approach, intuition, and complexity.

Medium

22. Generate Parentheses

Topic Dynamic Programming
Area Algorithms

if the parenthesis is correct, each parenthesis must be closed. which means, each closed parenthesis can be replaced with '()', that must be also complete. and

Medium

5. Longest Palindromic Substring

Topic Dynamic Programming
Area Algorithms

the approach was simple. I wanted to make it time complexity so I tried to check possible pelindrome that has center on i-th element. as it written on the hint,

Easy

1518. Water Bottles

Topic Math
Area Algorithms

i focused on the step of the process. in the problem, there are two big steps. which are drink and exchange.the order of those steps doen't really matter so I p

Medium

1039. Minimum Score Triangulation of Polygon

Topic Dynamic Programming
Area Algorithms

the apprach was correct. you must divide the big problem into sub problems, and solve them. so if you choose one triangle from a polygon, the rest part will be

Medium

2221. Find Triangular Sum of an Array

Topic Math
Area Algorithms

i used recursion, if the array length is 1, it returns the number. and if else, we do the calculation and reduce the size of array by one.

Medium

7. Reverse Integer

Topic Math
Area Algorithms

first, I extract its negative sign, and make a reversing function. and then, it manage the errors that can occur becuase of overflow.

Medium

8. String to Integer (atoi)

Topic String
Area Algorithms

I followed the instruction from the question. it removes the leading spaces, and find the sign symbol. then, read the number one by one. if wrong letter appears

Medium

11. Container With Most Water

Topic Two Pointers
Area Algorithms

it focus on the possibility of growing. I dont' know how to show mathematically, but it works like this. there are two pointers from left and right ends. you ca

Medium

12. Integer to Roman

Topic Math
Area Algorithms

first i found out the max length of it is 15 so i prepared for a string memory of 16 chars. 1 is for char. then, I build up the roman number one by one. you

Medium

6. Zigzag Conversion

Topic String
Area Algorithms

I apprached this question by thinking i'm actually writing the string with zigzag patterns. so I made a list for each rows, and add one charanter one by one on

Easy

412. Fizz Buzz

Topic Math
Area Algorithms

easy approach. just add correct string to the list and return it. to get the correct, string use conditional statements.

Easy

387. First Unique Character in a String

Topic Hash Table
Area Data Structures

I used the easier way to solve this. it uses two while loops to findout it has pair or not. so for each index, it goes trough whole string and find its pair. if

Easy

389. Find the Difference

Topic Bit Manipulation
Area Algorithms

we know using xor operator, this problem can be solved super easily. mathematically, there are three properties of xor operator that can show this code is the s

Easy

392. Is Subsequence

Topic Dynamic Programming
Area Algorithms

on the question, it explains the subsequence is just same list with some deleted characters. so I go through the string and comparing them while ignoring delete

Easy

404. Sum of Left Leaves

Topic Depth-First Search
Area Data Structures

as I said management should be done on the grandpa level node. lets don't think this now and build a base cases for recursion. 1. if root is null -> return 0 2.

Easy

374. Guess Number Higher or Lower

Topic Binary Search
Area Algorithms

binary search algorithm works for this problem. guess the middle value and depends on the answer, you update the possible range of the picked number. if there a

Easy

383. Ransom Note

Topic Hash Table
Area Algorithms

i still don't know what are ransomNote and magazines are but I thought they are like ingredients and the food. so to make a food of 'aab', you need two 'a' and

Easy

342. Power of Four

Topic Bit Manipulation
Area Algorithms

the approach is simple, if the number is power of four, only 1 will left if you devide the number by 4 repeatedly. and i used bit manipulation to measure it's m

Easy

344. Reverse String

Topic Two Pointers
Area Algorithms

i could just switch first and last character, and second and second last character, and so on. you don't need to consider whether it has even or odd length, bec

Easy

345. Reverse Vowels of a String

Topic Two Pointers
Area Algorithms

to solve this problem, i used two pointer approach. there are two pointers on the string, one starts from the front and the other starts from the back. they bot

Easy

349. Intersection of Two Arrays

Topic Binary Search
Area Algorithms

python has wonderful datatype 'set' that supports all the mathematical functions of set. it uses some hash map method, and it's pretty hard to build it my own o

Easy

367. Valid Perfect Square

Topic Binary Search
Area Algorithms

just compare the num with all the perfect squares from 1 to max.

Easy

292. Nim Game

Topic Math
Area Algorithms

you can definitely win if your friends has only 4 stones left. because if he takes 1, you can take all 3 and win. if he takes 2, you can take all 2 and win. if

Easy

303. Range Sum Query - Immutable

Topic Array
Area Algorithms

I took the easiest apprach. the array is just an array, and the sum finds the sum using for loop.

Easy

326. Power of Three

Topic Math
Area Algorithms

compare the number with all the possible power of threes.

Easy

338. Counting Bits

Topic Dynamic Programming
Area Algorithms

0 --> 0 (0) 1 --> 1 (1) 2 --> 10 (1) 3 --> 11 (2) 4 --> 100 (1) 5 --> 101 (2) 6 --> 110 (2) 7 --> 111 (3) 8 --> 1000 (1) 9 --> 1001 (2) 10--> 1010 (2) 11--> 101

Easy

278. First Bad Version

Topic Binary Search
Area Algorithms

as i mentioned, the binary search algorithm works well. determine the middle value is bad or good, and reduce the size of set more and more. I struggled a bit o

Easy

283. Move Zeroes

Topic Two Pointers
Area Algorithms

there is a fast pointer and slow pointer. they both start from 0 and fast pointer just rush to the end and just read the values. at the same time, slow pointer

Easy

290. Word Pattern

Topic Hash Table
Area Algorithms

the first part shows the string has unique pair to pattern and second part shows the pattern has uniqe pair to the string. it works well.

Easy

257. Binary Tree Paths

Topic Backtracking
Area Data Structures

As i mentioned, I used recursion. case 1 when its empty tree, it returns empty array. case 2 when it is a leaf node, [val] is the output. case 3 else, its addit

Easy

258. Add Digits

Topic Math
Area Algorithms

if it is single digit, return it. or, do the same process.

Easy

263. Ugly Number

Topic Math
Area Algorithms

if n = 1, return true if n is multiple of 2, run the same function on n/2 if n is multiple of 3, run the same function on n/3 if n is multiple of 5, run the sam

Easy

268. Missing Number

Topic Binary Search
Area Algorithms

if the array contains 0 to n with one missing number k, the sum of the array will be by the Gauss's method. so I calculated k by reverse.

Easy

231. Power of Two

Topic Bit Manipulation
Area Algorithms

if n is power of 2, in binary it will look like 0001000...0 then, if you do n-1, it will look like 00001111...1 then, if you take & operator to them, it will al

Easy

234. Palindrome Linked List

Topic Two Pointers
Area Data Structures

my apprach was like this. if we can find the midpoint of the linked list, we only need to reverse the first half and then compare it. it was a bit challenging t

Easy

222. Count Complete Tree Nodes

Topic Binary Search
Area Data Structures

the apprach is simple. it is just like counting algorithm for any tree, but it has some additional feature that returns if the tree is fully filled.

Easy

225. Implement Stack using Queues

Topic Stack
Area Data Structures

just created a node struct and made a stack. there's nothing to talk about this. this is so classic.

Easy

226. Invert Binary Tree

Topic Depth-First Search
Area Data Structures

using recursion to solve this. flip the left and flip the right and switch left and right. that's all.

Easy

219. Contains Duplicate II

Topic Sliding Window
Area Algorithms

let's say the array is an array of balls that has numbers on them. and trying to find two balls that are close and has the same number. what I can do is take a

Easy

205. Isomorphic Strings

Topic Hash Table
Area Algorithms

first, we convert both strings into the list of integers. the same letter will be replaced to the same number. for example, 'FOO' will be '011', and 'BAR' will

Easy

206. Reverse Linked List

Topic Recursion
Area Data Structures

just travel trough linked list and change the next value as you move one.

Easy

202. Happy Number

Topic Two Pointers
Area Algorithms

according to the wikipedia, only one and seven are happy number. And I don't know how but, every cycle will contain at least one single digit number. so I loope

Easy

169. Majority Element

Topic Divide and Conquer
Area Algorithms

there is nothing to say about the apprach. just read this wikipedia page. https://en.wikipedia.org/wiki/Boyer%E2%80%93Mooremajorityvotealgorithm

Easy

171. Excel Sheet Column Number

Topic Math
Area Algorithms

it calls the help function. I created the help function to send the length of string as an argument of function. Otherwise, it would increase the complexity by

Easy

190. Reverse Bits

Topic Divide and Conquer
Area Algorithms

save each digits in hash, and re-build it.

Easy

191. Number of 1 Bits

Topic Divide and Conquer
Area Algorithms

from moving on each bits, count the number of ones.

Easy

168. Excel Sheet Column Title

Topic Math
Area Algorithms

this question sucks. it's not even converting base question. and it is so stupid to solve this with C

Easy

125. Valid Palindrome

Topic Two Pointers
Area Algorithms

The approach to this problem is very intuitive. firstly, the string might contain unnecessary letters such as spaces, quotemark, or colon or something. therefor

Easy

136. Single Number

Topic Bit Manipulation
Area Algorithms

The approach to this problem is more Mathematical. There are four key property of XOR operator to solve this problem. 1. identity of XOR is 0 : 2. XOR is Commut

Easy

144. Binary Tree Preorder Traversal

Topic Depth-First Search
Area Data Structures

so, firstly I need to prepare for the memory, so I made a function that counts the of nodes of the tree. than malloc an array that can carry that amount of inte

Easy

145. Binary Tree Postorder Traversal

Topic Depth-First Search
Area Data Structures

this is the same question with previous one so I just copy and pasted it. There will be nothing to say about this. if you want more info about this, goto 026.pr

Easy

121. Best Time to Buy and Sell Stock

Topic Dynamic Programming
Area Algorithms

the approach is pretty complex, but here me out. from scanning the prices from the beginning, it find the starting point. if you find the smaller starting point

Easy

118. Pascal's Triangle

Topic Dynamic Programming
Area Algorithms

Firstly, I focused more on creating the memory thing, using malloc, and then started to put numbers in that. It was building the triangle from the top, so I cou

Easy

119. Pascal's Triangle II

Topic Dynamic Programming
Area Algorithms

I made two array that does exactly same thing with previous question.

Easy

69. Sqrt(x)

Topic Binary Search
Area Algorithms

the apprach is very stupid, but works. it itterate from 1 to the number itself, until the number is greater than target num devided by the number. it works!

Easy

70. Climbing Stairs

Topic Dynamic Programming
Area Algorithms

I could find out two apprachs. one is using recursion, the other is using loop. I thought second one looks more efficient, so I did with that. it's like fibonac

Easy

83. Remove Duplicates from Sorted List

Topic Linked List
Area Data Structures

from the beginning, It look at the next node, and it have the same value with current one, it removes the second one. that's all.

Easy

88. Merge Sorted Array

Topic Two Pointers
Area Algorithms

Therefore, I decided to build the merged array from the behind. the answer will be build from the biggest number with non-increasing order. this worked well. th

Easy

94. Binary Tree Inorder Traversal

Topic Depth-First Search
Area Data Structures

first, I have to find out how long will the answer be, which is number of nodes, so I made count tree function. Then, I tried to make reculsive function, but pu

Easy

100. Same Tree

Topic Depth-First Search
Area Data Structures

two trees are identical when their values and two childs are identical. we can use reculsion for that easily.

Easy

67. Add Binary

Topic Bit Manipulation
Area Algorithms

The second apprach is reversing the binary string. by reversing them, the problem occured from approach 1 is almost solved. because after the reversion, head of

Easy

35. Search Insert Position

Topic Binary Search
Area Algorithms

The approach to this question is binary search for sorted array. so I used idea of sub-array and offset of that array relative to the original one. and that wor

Easy

58. Length of Last Word

Topic String
Area Algorithms

As I described on the intuition part, i made a variable called 'prev', which stores previous character. the initial value of it should be a space, so that s sta

Easy

66. Plus One

Topic Math
Area Algorithms

Firstly, it was necessary to find out how much memory the answer will take. if the number is with tons of 9s, it will need new digit. Therefore, I made a functi

Easy

26. Remove Duplicates from Sorted Array

Topic Two Pointers
Area Algorithms

the apprach to this problem was I wanted to use a for loop. because the job is pretty simple, so I could done it by reading through the list one by one. and it

Easy

27. Remove Element

Topic Two Pointers
Area Algorithms

same with 008 question but compare the value with val. not prev.

Easy

111. Minimum Depth of Binary Tree

Topic Depth-First Search
Area Data Structures

I also used reculsive algorithm to this problem, just like others. I love reculsive functions. in this case, the function should return 1 when the tree is the l

Easy

112. Path Sum

Topic Depth-First Search
Area Data Structures

first, it returns false if the tree is empty. no tree means no path. and if it is leaf and target is the value, it returns true. or, it calls their child for th

Easy

141. Linked List Cycle

Topic Two Pointers
Area Data Structures

for every node on the linked list, it loop through the whole linked list and comparing with the next node of the node I'm looking at.which has horrible time com

Easy

110. Balanced Binary Tree

Topic Depth-First Search
Area Data Structures

As I mentioned above, the definition of balanced tree contains the concept of height of a tree inside. therefore, I thought the calculating height was essential

Easy

108. Convert Sorted Array to Binary Search Tree

Topic Divide and Conquer
Area Data Structures

As i mentioned from the intuition part, the apprach was pretty simple. I first found out I need to deal with the middle value of the sorted array since It shoul

Easy

101. Symmetric Tree

Topic Depth-First Search
Area Data Structures

As I said, I decided to break down the prooblem into two different parts. First was fliping one side of the tree, and another part is comparing two trees. if a

Easy

104. Maximum Depth of Binary Tree

Topic Depth-First Search
Area Data Structures

I used the reculsive method to solve the problem. Firstly, I found the base case, where the the null tree has zero depth, and the depth of a tree is its deepest