Tag Archives: leetcode

Leetcode çözümleri | Maximum Product Subarray

Leetcode içerisinde bulunan “Maximum Product Subarray” sorusunun açıklaması ve çözümü. Bu soruda size verilen tam sayısı dizisi için, içerisindeki subarray’lerde (birbirini takip eden elemanlardan oluşan) çarpımı en fazla olan subarray’in eleman çarpımını döndürmeniz isteniyor.

➡️ Leetcode 152. Maximum Product Subarray: https://leetcode.com/problems/maximum-product-subarray/

➡️ Problem açıklaması:

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

A subarray is a contiguous subsequence of the array.

Example 1:

Input: nums = [2,3,-2,4]

Output: 6

Explanation: [2,3] has the largest product 6.

Example 2:

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

Output: 0

Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

Constraints:

1 <= nums.length <= 2 * 10^4

-10 <= nums[i] <= 10

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

Leetcode çözümleri | Maximum Subarray | Kadane Algoritması

Leetcode içerisinde bulunan “Maximum Subarray” sorusunun açıklaması ve çözümü. Bu soruda size verilen tam sayısı dizisi için, içerisindeki subarray’lerde (birbirini takip eden elemanlardan oluşan) toplamı en fazla olan array’in eleman toplamını döndürmeniz isteniyor.

➡️ Leetcode 53. Maximum Subarray: https://leetcode.com/problems/maximum-subarray/

➡️ Problem açıklaması:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

A subarray is a contiguous part of an array.

Example 1:

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

Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]

Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]

Output: 23

Constraints:

1 <= nums.length <= 10^5

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

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Kaynaklar: https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane’s_algorithm

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?