Tag Archives: leetcode

Leetcode Çözümleri | Three Consecutive Odds

Leetcode içerisinde bulunan “Three Consecutive Odds” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisinde, 3 tane yan yana tek sayı olup olmadığını bulmanız isteniyor.

➡️ Leetcode 1550. Three Consecutive Odds: https://leetcode.com/problems/three-consecutive-odds/

➡️ Problem açıklaması:

Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.

Example 1:

Input: arr = [2,6,4,1]

Output: false

Explanation: There are no three consecutive odds.

Example 2:

Input: arr = [1,2,34,3,4,5,7,23,12]

Output: true

Explanation: [5,7,23] are three consecutive odds.

Constraints:

1 <= arr.length <= 1000

1 <= arr[i] <= 1000

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

Leetcode Çözümleri – Shuffle String

Leetcode içerisinde bulunan “Shuffle String” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string‘in her bir karakteri, yine bir tam sayı dizisi olarak verilen index’lerde olacak şekilde shuffle etmeniz isteniyor.

► Leetcode 1528. Shuffle String: https://leetcode.com/problems/shuffle-string/

► Problem açıklaması:

Given a string s and an integer array indices of the same length.

The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

Example 1:

Input: s = “codeleet”, indices = [4,5,6,7,0,2,1,3]

Output: “leetcode”

Explanation: As shown, “codeleet” becomes “leetcode” after shuffling.

Example 2:

Input: s = “abc”, indices = [0,1,2]

Output: “abc”

Explanation: After shuffling, each character remains in its position.

Example 3:

Input: s = “aiohn”, indices = [3,1,4,2,0]

Output: “nihao”

Example 4:

Input: s = “aaiougrt”, indices = [4,0,2,6,7,3,1,5]

Output: “arigatou”

Example 5:

Input: s = “art”, indices = [1,0,2]

Output: “rat”

Constraints:

s.length == indices.length == n

1 <= n <= 100

s contains only lower-case English letters.

0 <= indices[i] < n

All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n – 1).

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