Coding Test/LeetCode
-
[LeetCode] 590. N-ary Tree Postorder Traversal c++Coding Test/LeetCode 2022. 11. 29. 18:51
문제 Given the root of an n-ary tree, return the postorder traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples) Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [5,6,3,2,4,1] 문제 해결 DFS문제 class Solution { public: vector postorder(Node* root) { if(!root){ return {}; } ..
-
[LeetCode] 1971. Find if Path Exists in Graph c++Coding Test/LeetCode 2022. 11. 29. 17:57
문제 There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. You want to determine if there is a v..
-
[LeetCode] 1672. Richest Customer Wealth c++Coding Test/LeetCode 2022. 10. 10. 16:43
문제 You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has. A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth. Input: accounts = [[1,2,3],[3,2,1]] Output..
-
[LeetCode] 1470. Shuffle the Array c+Coding Test/LeetCode 2022. 10. 10. 16:31
문제 Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn]. Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. 문제 해결 class Solution { public: vector shuffle(vector& nums, int n) { vector answer; for(int i = 0; i < n; i+..
-
[LeetCode] 2011. Final Value of Variable After Performing Operations c++Coding Test/LeetCode 2022. 10. 10. 16:06
문제 There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations. Input: operations = ["--X","X++","X+..
-
[LeetCode] 35. Search Insert Position c++Coding Test/LeetCode 2022. 10. 10. 13:41
문제 Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Input: nums = [1,3,5,6], target = 5 Output: 2 배열 원소 중 target과 같은 값이 있다면 해당 index 구하고, 없다면 오름차순인 배열에 위치시킬 index를 구하는 문제이다. 문제 해결 이 문제를 해결하기 위해 lower_bound 함수에..
-
[LeetCode] 1480. Running Sum of 1d Array c++Coding Test/LeetCode 2022. 10. 10. 13:20
문제 Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. 문제 해결 class Solution { public: vector runningSum(vector& nums) { int len = nums.size(); vector answer(len, 0); answer[0] = nums[0]; for(int i = 1; ..