Category Archives: LeetCode

Leetcode Çözümleri | Valid Palindrome II

Leetcode içerisinde bulunan “Valid Palindrome II” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string için, en fazla 1 karakter silerek palindrom yapılıp yapılamayacağı soruluyor. En fazla denildiği için hiç karakter silmeyebilirsiniz tabi.

➡️ Leetcode Valid Palindrome II: https://leetcode.com/problems/valid-palindrome-ii/

➡️ Problem açıklaması:

Given a string s, return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = “aba”

Output: true

Example 2:

Input: s = “abca”

Output: true

Explanation: You could delete the character ‘c’.

Example 3:

Input: s = “abc”

Output: false

Constraints:

1 <= s.length <= 10^5

s consists of lowercase English letters.

Leetcode Çözümleri | Binary Search

Leetcode içerisinde bulunan “Binary Search” sorusunun açıklaması ve çözümü. Bu soruda size artan sırada verilen bir tam sayı dizisinde, O(log*n) çalışma zamanında olan bir algoritma ile verilen bir tam sayının index’ini, bu tam sayı dizide yoksa -1 döndürmeniz isteniyor. Kısaca binary search algoritmasını implement etmeniz isteniyor.

➡️ LeetCode 704. Binary Search: https://leetcode.com/problems/binary-search/

➡️ Problem açıklaması:

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9

Output: 4

Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2

Output: -1

Explanation: 2 does not exist in nums so return -1

Constraints:

1 <= nums.length <= 10^4

-10^4 < nums[i], target < 10^4

All the integers in nums are unique.

nums is sorted in ascending order.

Leetcode Çözümleri | Binary Tree Inorder Traversal

Leetcode içerisinde bulunan “Binary Tree Inorder Traversal” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘nin node’larını inorder traversal, yani öncelikle rekürsif olarak sol tarafı, sonra head nodu, sonra da rekürsif olarak sağ tarafı geri döndürmeniz isteniyor.

➡️ Tree traversal: https://en.wikipedia.org/wiki/Tree_traversal

➡️ Leetcode 94. Binary Tree Inorder Traversal: https://leetcode.com/problems/binary-tree-inorder-traversal/

➡️ Problem açıklaması:

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1:

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

Output: [1,3,2]

Example 2:

Input: root = []

Output: []

Example 3:

Input: root = [1]

Output: [1]

Constraints:

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

-100 <= Node.val <= 100

Follow up: Recursive solution is trivial, could you do it iteratively?

Leetcode Çözümleri | 3SUM

Leetcode içerisinde bulunan “3Sum” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisinde, toplamları 0 olan üçlü sayı kümelerini tekil (distinct) olarak bulmanız isteniyor.

➡️ Videoda bahsettiğim “Two Sum” problemi: https://youtu.be/cHDvgJK79sI

➡️ LeetCode 15. 3Sum: https://leetcode.com/problems/3sum/

➡️ Problem açıklaması:

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Example 1:

Input: nums = [-1,0,1,2,-1,-4]

Output: [[-1,-1,2],[-1,0,1]]

Example 2:

Input: nums = []

Output: []

Example 3:

Input: nums = [0]

Output: []

Constraints:

0 <= nums.length <= 3000

-10^5 <= nums[i] <= 10^5

Leetcode Çözümleri – Evaluate Reverse Polish Notation

Leetcode içerisinde bulunan “Evaluate Reverse Polish Notation” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “reverse polish notation”‘ın sonucunu geriye döndürmeniz isteniyor.

➡️ Reverse Polish Notation nedir? https://en.wikipedia.org/wiki/Reverse_Polish_notation

➡️ LeetCode 150. Evaluate Reverse Polish Notation: https://leetcode.com/problems/evaluate-reverse-polish-notation/

➡️ Problem açıklaması:

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

Example 1:

Input: tokens = [“2″,”1″,”+”,”3″,”*”]

Output: 9

Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: tokens = [“4″,”13″,”5″,”/”,”+”]

Output: 6

Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: tokens = [“10″,”6″,”9″,”3″,”+”,”-11″,”*”,”/”,”*”,”17″,”+”,”5″,”+”]

Output: 22

Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5

= ((10 * (6 / (12 * -11))) + 17) + 5

= ((10 * (6 / -132)) + 17) + 5

= ((10 * 0) + 17) + 5

= (0 + 17) + 5

= 17 + 5

= 22

Constraints:

1 <= tokens.length <= 10^4

tokens[i] is either an operator: “+”, “-“, “*”, or “/”, or an integer in the range [-200, 200].

Microsoft Mülakat Sorusu | Insertion Sort List

Leetcode içerisinde bulunan “Insertion Sort List” sorusunun açıklaması ve çözümü. Bu soruda size verilen linked list içerisindeki node’ları insertion sort kullanarak sıralamanız isteniyor.

➡️ Leetcode 147. Insertion Sort List: https://leetcode.com/problems/insertion-sort-list/

➡️ Problem açıklaması:

Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head. The steps of the insertion sort algorithm:

– Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.

– At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.

– It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

Example 1:

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

Output: [1,2,3,4]

Example 2:

Input: head = [-1,5,3,4,0]

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

Constraints:

The number of nodes in the list is in the range [1, 5000].

-5000 <= Node.val <= 5000

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