Tag Archives: leetcode soruları

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 – 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 – Sum of Digits in Base K

LeetCode içerisinde bulunan “Sum of Digits in Base K” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir sayının, verilen bir taban’da karşılığının rakamları toplamı soruluyor.

► LeetCode 1837. Sum of Digits in Base K: https://leetcode.com/problems/sum-of-digits-in-base-k/

► Problem açıklaması:

Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.

After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.

Example 1:

Input: n = 34, k = 6

Output: 9

Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Example 2:

Input: n = 10, k = 10

Output: 1

Explanation: n is already in base 10. 1 + 0 = 1.

Constraints:

1 <= n <= 100

2 <= k <= 10

Amazon ve Microsoft Mülakat Sorusu – Letter Combinations of a Phone Number

LeetCode içerisinde bulunan “Letter Combinations of a Phone Number” sorusunun açıklaması ve çözümü. Bu soruda size bir cep telefonu üzerinde basılacak olan tuşların listesi verilip, olası yazılabilecek tüm mesaj kombinasyonları bir liste olarak geri döndürmeniz isteniyor.

► LeetCode 17. Letter Combinations of a Phone Number: https://leetcode.com/problems/letter-combinations-of-a-phone-number/

► Problem açıklaması:

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example 1:

Input: digits = “23”

Output: [“ad”,”ae”,”af”,”bd”,”be”,”bf”,”cd”,”ce”,”cf”]

Example 2:

Input: digits = “”

Output: []

Example 3:

Input: digits = “2”

Output: [“a”,”b”,”c”]

Constraints:

0 <= digits.length <= 4

digits[i] is a digit in the range [‘2’, ‘9’].

LeetCode Çözümleri – Thousand Separator

LeetCode içerisinde bulunan “Thousand Separator” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı için geriye binlik ayıracı olarak nokta koyduğunuz string halini döndürmeniz isteniyor.

Maalesef videoyu kaydederken bazı donmalar olmuş, kusura bakmayın.

► LeetCode 1556. Thousand Separator: https://leetcode.com/problems/thousand-separator/

► Problem açıklaması:

Given an integer n, add a dot (“.”) as the thousands separator and return it in string format.

Example 1:

Input: n = 987

Output: “987”

Example 2:

Input: n = 1234

Output: “1.234”

Example 3:

Input: n = 123456789

Output: “123.456.789”

Example 4:

Input: n = 0

Output: “0”

Constraints:

0 <= n < 2^31

LeetCode Çözümleri – 1185. Day of the Week [Zeller’s congruence]

LeetCode içerisinde bulunan “Day of the Week” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen gün, ay ve yıl bilgilerine ait tarihin haftanın hangi güne denk geldiğini bulmanız isteniyor.

► LeetCode 1185. Day of the Week: https://leetcode.com/problems/day-of-the-week/

► Zeller’s congruence https://en.wikipedia.org/wiki/Zeller%27s_congruence

► Problem açıklaması:

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}.

Example 1: Input: day = 31, month = 8, year = 2019 Output: “Saturday”

Example 2: Input: day = 18, month = 7, year = 1999 Output: “Sunday”

Example 3: Input: day = 15, month = 8, year = 1993 Output: “Sunday”

Constraints: The given dates are valid dates between the years 1971 and 2100.

LeetCode Çözümleri – 796. Rotate String

LeetCode içerisinde bulunan “Rotate String” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen iki string‘ten, ilk string herhangi bir sayıda rotasyon ile ikinci string’e dönüşüp dönüşemeyeceği soruluyor.

► LeetCode 796. Rotate String: https://leetcode.com/problems/rotate-string/

► Problem açıklaması:

We are given two strings, s and goal.

A shift on s consists of taking string s and moving the leftmost character to the rightmost position. For example, if s = ‘abcde’, then it will be ‘bcdea’ after one shift on s. Return true if and only if s can become goal after some number of shifts on s.

Example 1:

Input: s = ‘abcde’, goal = ‘cdeab’

Output: true

Example 2:

Input: s = ‘abcde’, goal = ‘abced’

Output: false

Note:

s and goal will have length at most 100.

LeetCode Çözümleri – 1544. Make The String Great

LeetCode içerisinde bulunan “Make The String Great” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string‘i, “iyi” bir string yapmanız gerekiyor. Bu da string’i, birbirini takip eden aynı karakterlerden – fakat biri küçük diğer büyük harf olacak şekilde – arındırmanız demek.

► LeetCode 1544. Make The String Great: https://leetcode.com/problems/make-the-string-great/

► Problem açıklaması:

Given a string s of lower and upper case English letters.

A good string is a string which doesn’t have two adjacent characters s[i] and s[i + 1] where:

0 <= i <= s.length – 2

s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

Example 1:

Input: s = “leEeetcode”

Output: “leetcode”

Explanation: In the first step, either you choose i = 1 or i = 2, both will result “leEeetcode” to be reduced to “leetcode”.

Example 2:

Input: s = “abBAcC”

Output: “”

Explanation: We have many possible scenarios, and all lead to the same answer. For example:

“abBAcC” — “aAcC” — “cC” — “”

“abBAcC” — “abBA” — “aA” — “”

Example 3:

Input: s = “s”

Output: “s”