Coding Test/LeetCode
-
[LeetCode] 653. Two Sum IV - Input is a BST c++Coding Test/LeetCode 2023. 2. 22. 18:43
문제 Given the root of a binary search tree and an integer k, return true if there exist two elements in the BST such that their sum is equal to k, or false otherwise. 문제 해결 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} ..
-
[LeetCode] 1464. Maximum Product of Two Elements in an Array c++Coding Test/LeetCode 2023. 2. 21. 15:47
문제 Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1). Input: nums = [3,4,5,2] Output: 12 Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 문제 해결 class Solution { public: int maxProduct(vector..
-
[LeetCode] 206. Reverse Linked List c++Coding Test/LeetCode 2023. 1. 4. 23:00
문제 Given the head of a singly linked list, reverse the list, and return the reversed list. 단일 링크드 리스트가 주어지면 순서를 뒤집는 문제입니다. 문제 해결 class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *nextNode, *prevNode = NULL; while(head){ nextNode = head->next; head->next = prevNode; prevNode = head; head = nextNode; } return prevNode; } }; 다른 풀이 class Solution { public: ListNode* reverseL..
-
[LeetCode] 21. Merge Two Sorted Lists c++Coding Test/LeetCode 2023. 1. 4. 22:47
문제 You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. 주어진 두 개의 노드를 오름차순으로 합치는 문제입니다. 문제 해결 RECURSIVE APPROACH class Solution { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { if(NULL == list1){ ret..
-
[LeetCode] 994. Rotting Oranges c++Coding Test/LeetCode 2023. 1. 4. 19:10
문제 You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. 2는 썩..
-
[LeetCode] 542. 01 Matrix c++Coding Test/LeetCode 2023. 1. 3. 17:15
문제 Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. 문제 해결 벡터값 초기화 값을 INT8_MAX로 하니 최대값이 127이라 fail이 나왔다. 1 = 0 && y res[curx][cury] + 1){ res[x][y] = res[curx][cury] + 1; q.push({x, y}); } } } } return res; } };
-
[LeetCode] 116. Populating Next Right Pointers in Each Node c++Coding Test/LeetCode 2023. 1. 2. 23:12
문제 You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: next pointer 를 next right node를 가르키도록 다음 포인터를 채웁니다. next right node가 없으면 null pointer로 설정합니다. 문제 해결 Node* connect(Node* root) { //base case if (root == NULL) return NULL; //connects the left subtree of same level with right subtree of tha..
-
[LeetCode] 695. Max Area of Island c++Coding Test/LeetCode 2022. 12. 28. 21:25
문제 You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. The area of an island is the number of cells with a value 1 in the island. Return the maximum area of an island in grid. If there is no island, return 0. 배열의 요소 값이 1이고, 1이 상하좌우로 연속되어 있을..