Category Archives: LeetCode

LeetCode Çözümleri | Add Digits

Leetcode içerisinde bulunan “Add Digits” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayının rakamları toplamı tek hane olana kadar toplayıp geriye döndürmeniz isteniyor.

➡️ LeetCode 258. Add Digits: https://leetcode.com/problems/add-digits/

➡️ Problem açıklaması:

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Example 1:

Input: num = 38

Output: 2

Explanation: The process is

38 —- 3 + 8 —- 11

11 —- 1 + 1 —- 2

Since 2 has only one digit, return it.

Example 2:

Input: num = 0

Output: 0

Constraints:

0 <= num <= 2^31 – 1

⚠️Follow up: Could you do it without any loop/recursion in O(1) runtime?

Leetcode Çözümleri | Determine Color of a Chessboard Square

Leetcode içerisinde bulunan “Determine Color of a Chessboard Square” sorusunun açıklaması ve çözümü. Bu soruda size verilen satranç tahtası üzerindeki karenin beyaz bir kare olup olmadığını bulmanız isteniyor.

➡️ Leetcode 1812. Determine Color of a Chessboard Square: https://leetcode.com/problems/determine-color-of-a-chessboard-square/

➡️ Problem açıklaması:

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

Example 1:

Input: coordinates = “a1”

Output: false

Explanation: From the chessboard above, the square with coordinates “a1” is black, so return false.

Example 2:

Input: coordinates = “h3”

Output: true

Explanation: From the chessboard above, the square with coordinates “h3” is white, so return true.

Example 3:

Input: coordinates = “c7”

Output: false

Constraints:

coordinates.length == 2

‘a’ <= coordinates[0] <= ‘h’

‘1’ <= coordinates[1] <= ‘8’

Bloomberg Mülakat Sorusu – Matrix Diagonal Sum

Leetcode içerisinde bulunan “Matrix Diagonal Sum” sorusunun açıklaması ve çözümü. Bu soruda sizi jagged array olarak verilen bir matris içerisindeki iki diagonalde bulunan elemanların toplamını bulmanız isteniyor.

► Leetcode 1572. Matrix Diagonal Sum: https://leetcode.com/problems/matrix-diagonal-sum/

► Problem açıklaması:

Given a square matrix mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

Example 1:

Input:

mat = [[1,2,3], [4,5,6], [7,8,9]]

Output: 25

Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 Notice that element mat[1][1] = 5 is counted only once.

Example 2:

Input:

mat = [[1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1]]

Output: 8

Example 3:

Input:

mat = [[5]]

Output: 5

Constraints: n == mat.length == mat[i].length

1 <= n <= 100

1 <= mat[i][j] <= 100

Leetcode kolay seviyede Linked List soruları – 1. Kısım

Leetcode içerisindeki Linked List veri yapısı ile ilgili kolay soruları çözüyoruz.

Çözdüğümüz soruların linkleri:

1290. Convert Binary Number in a Linked List to Integer: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

876. Middle of the Linked List: https://leetcode.com/problems/middle-of-the-linked-list/

206. Reverse Linked List: https://leetcode.com/problems/reverse-linked-list/

21. Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/

Leetcode Çözümleri – Sum of Unique Elements

Leetcode içerisinde bulunan “Sum of Unique Elements” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisi içerisindeki unique elemanların toplamını bulmanız isteniyor.

► LeetCode 1748. Sum of Unique Elements: https://leetcode.com/problems/sum-of-unique-elements/

► Problem açıklaması:

You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

Example 1:

Input: nums = [1,2,3,2]

Output: 4

Explanation: The unique elements are [1,3], and the sum is 4.

Example 2:

Input: nums = [1,1,1,1,1]

Output: 0

Explanation: There are no unique elements, and the sum is 0.

Example 3:

Input: nums = [1,2,3,4,5]

Output: 15

Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.

Constraints:

1 <= nums.length <= 100

1 <= nums[i] <= 100

Facebook Mülakat Sorusu – Lowest Common Ancestor of a Binary Search Tree

Leetcode içerisinde bulunan “Lowest Common Ancestor of a Binary Search Tree” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary search tree içerisinde, verilen iki tane TreeNode’un en aşağıdaki ortak parent’ını bulmanız isteniyor. Bence deneme olarak bunun iterative halini de siz yazabilirsiniz.

► LeetCode 235. Lowest Common Ancestor of a Binary Search Tree: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

► Problem açıklaması:

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia:

“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8

Output: 6

Explanation: The LCA of nodes 2 and 8 is 6.

Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4

Output: 2

Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [2,1], p = 2, q = 1

Output: 2

Constraints:

The number of nodes in the tree is in the range [2, 105].

-109 <= The number of nodes in the tree is in the range [2, 10^5].

-10^9 <= Node.val <= 10^9

All Node.val are unique.

p != q

p and q will exist in the BST.

Microsoft Mülakat Sorusu – Odd Even Linked List

Leetcode içerisinde bulunan “Odd Even Linked List” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir linked list içerisinde, ilk node tek olacak şekilde önce tek index’teki node’ları, ardından çift index’teki node’lar gelecek şekilde düzenlemeniz isteniyor.

► LeetCode 328. Odd Even Linked List: https://leetcode.com/problems/odd-even-linked-list/

► Problem açıklaması:

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time complexity.

Example 1:

Input: head = [1,2,3,4,5]

Output: [1,3,5,2,4]

Example 2:

Input: head = [2,1,3,5,6,4,7]

Output: [2,3,6,7,1,5,4]

Constraints:

n == number of nodes in the linked list

0 <= n <= 10^4

-10^6 <= Node.val <= 10^6

Microsoft Mülakat Sorusu – Count Negative Numbers in a Sorted Matrix

LeetCode içerisinde bulunan “Count Negative Numbers in a Sorted Matrix” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir matrix içerisinde kaç adet negatif sayı olduğunu efektif bir şekilde bulmanız isteniyor.

► LeetCode 1351. Count Negative Numbers in a Sorted Matrix: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/

► Problem açıklaması:

Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.

Example 1:

Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]

Output: 8

Explanation: There are 8 negatives number in the matrix.

Example 2:

Input: grid = [[3,2],[1,0]]

Output: 0

Example 3:

Input: grid = [[1,-1],[-1,-1]]

Output: 3

Example 4:

Input: grid = [[-1]]

Output: 1

Facebook Mülakat Sorusu – Binary Tree Right Side View

LeetCode içerisinde bulunan “Binary Tree Right Side View” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘nin en sağ tarafındaki node‘ları yukarıdan aşağıda doğru olacak şekilde geri döndürmeniz isteniyor.

► LeetCode 199. Binary Tree Right Side View: https://leetcode.com/problems/binary-tree-right-side-view/

► Problem açıklaması:

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example 1:

Input: root = [1,2,3,null,5,null,4]

Output: [1,3,4]

Example 2:

Input: root = [1,null,3]

Output: [1,3]

Example 3: Input: root = []

Output: []

Constraints:

The number of nodes in the tree is in the range [0, 100].

-100 <= Node.val <= 100

Amazon Mülakat Sorusu – Set Matrix Zeroes

LeetCode içerisinde bulunan “Set Matrix Zeroes” sorusunun açıklaması ve çözümü. Bu soruda size verilen m * n matrisi içerisinde, eğer bir eleman 0 ise, o elemanın ait olduğu tüm satır ve sütunları da 0 yapmanız isteniyor.

► LeetCode 73. Set Matrix Zeroes: https://leetcode.com/problems/set-matrix-zeroes/

► Problem açıklaması:

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix.

You must do it in place.

Example 1:

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]

Output: [[1,0,1],[0,0,0],[1,0,1]]

Example 2:

Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]

Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Constraints:

m == matrix.length

n == matrix[0].length

1 <= m, n <= 200

-2^31 <= matrix[i][j] <= 2^31 – 1