본문 바로가기

LeetCode/2020 LeetCoding Challenge16

LeetCode. Arranging Coins Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of a 32-bit signed integer. -Summary- Solved using binary search. class Solution: def arrangeCoins(self, n: int) -> int: #k(k+1) // 2 2020. 7. 2.
LeetCode. Unique Paths Problem: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there? Above is a 7 x 3 grid. How many possible unique paths are there? Example: -Cod.. 2020. 6. 30.
LeetCode. Sum Root to Leaf Numbers Problem: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. -Summary- Used recursive DFS. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=Non.. 2020. 6. 27.
LeetCode. Count Complete Tree Nodes Problem: Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. Example: -Summary- Solve by recursion 1. if the root exists.. 2020. 6. 24.
LeetCode.Single Number II Problem: Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,3,2] Output: 3 Example 2: Input: [0,1,0,1,0,1,99] Output: 99 -Summary- Solved by using dictionary. 1. for each numbe.. 2020. 6. 23.
LeetCode.H-Index II Problem: Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each." Example: Input: citatio.. 2020. 6. 19.