본문 바로가기

[OJ.leetcode] flatten-binary-tree-to-linked-list https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 /* 대충 구현하여도 O(n)에 다 수행가능함. "문제는 어떻게 하면 elegant하게 구현할 수 있을까?" preorder로 돌면서, 1) parent의 leftchild가 있을 경우, left subtree의 rightmost 를 가리키는 포인터를 하나 두고, 2) parent->right를 righ.. 더보기
[OJ.leetcode] jump-game https://oj.leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. /* A[i]가 0인 elemant가 있을때, 이를 jump할 수 있는지 여부만 없.. 더보기
[OJ.leetcode] merge-sorted-array https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m andn respectively. /* 간단히 O(m+n) 알고리즘으로 해결하자면, mergesort에서 merge 알고리즘과 같이 하나씩 비교해서 채워나가고, 2개의 .. 더보기