Tag Archives: leetcode

LeetCode Çözümleri – 977. Squares of a Sorted Array

LeetCode içerisinde bulunan “Squares of a Sorted Array” sorusunun açıklaması ve çözümü. Bu soruda size verilen sıralı bir dizide, her elemanın yine dizi içerisinde sıralı olarak “in-place” şeklinde karelerinin atanması isteniyor.

► LeetCode 977. Squares of a Sorted Array: https://leetcode.com/problems/squares-of-a-sorted-array/

► Problem açıklaması:

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Example 1:

Input: nums = [-4,-1,0,3,10]

Output: [0,1,9,16,100]

Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]

Output: [4,9,9,49,121]

Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?

LeetCode Çözümleri – 1249. Minimum Remove to Make Valid Parentheses

LeetCode içerisinde bulunan “Minimum Remove to Make Valid Parentheses” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string içerisinde, en az kaç adet parantez silerek “geçerli” bir string oluşturabileceğiniz soruluyor.

Geçerli bir string’in tanımı olarak;

► Boş string ve sadece küçük harfler içeren, veya

► A ve B geçerli string’ler olup AB şeklinde olan veya

► A geçerli string olup (A) şeklinde olanlardan bahsedilmiş.

► LeetCode 1249. Minimum Remove to Make Valid Parentheses: https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/

► Problem açıklaması:

Given a string s of ‘(‘ , ‘)’ and lowercase English characters. Your task is to remove the minimum number of parentheses ( ‘(‘ or ‘)’, in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

It is the empty string, contains only lowercase characters, or

It can be written as AB (A concatenated with B), where A and B are valid strings, or

It can be written as (A), where A is a valid string.

Example 1:

Input: s = “lee(t(c)o)de)”

Output: “lee(t(c)o)de”

Explanation: “lee(t(co)de)” , “lee(t(c)ode)” would also be accepted.

Example 2:

Input: s = “a)b(c)d”

Output: “ab(c)d”

Example 3:

Input: s = “))((”

Output: “”

Explanation: An empty string is also valid.

Example 4:

Input: s = “(a(b(c)d)”

Output: “a(b(c)d)”

Constraints:

1 <= s.length <= 10^5

s[i] is one of ‘(‘ , ‘)’ and lowercase English letters.

LeetCode Çözümleri – 9. Palindrome Number

LeetCode içerisinde bulunan “Palindrome Number” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayının palindrom olup olmadığı soruluyor. Palindrom, tersten okunuşu da aynı olan cümle, sözcük ve sayılara denilmektedir.

► Palindrom nedir? https://tr.wikipedia.org/wiki/Palindrom

► LeetCode 9. Palindrome Number: https://leetcode.com/problems/palindrome-number/

► Problem açıklaması:

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121

Output: true

Example 2:

Input: x = -121

Output: false

Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101

Output: false

Constraints:

-2^31 lessEqual x lessEqual 2^31 – 1

LeetCode Çözümleri – 1588. Sum of All Odd Length Subarrays

LeetCode içerisinde bulunan “Sum of All Odd Length Subarrays” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisinde, tüm tek uzunluktaki alt dizilerin (birbirini takip eden elemanların oluşturduğu) toplamını bulmanız isteniyor.

► LeetCode 1588. Sum of All Odd Length Subarrays: https://leetcode.com/problems/sum-of-all-odd-length-subarrays/

► Problem açıklaması:

Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.

A subarray is a contiguous subsequence of the array.

Return the sum of all odd-length subarrays of arr.

Example 1:

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

Output: 58

Explanation: The odd-length subarrays of arr and their sums are:

[1] = 1

[4] = 4

[2] = 2

[5] = 5

[3] = 3

[1,4,2] = 7

[4,2,5] = 11

[2,5,3] = 10

[1,4,2,5,3] = 15

If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58

Example 2:

Input: arr = [1,2]

Output: 3

Explanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.

Example 3:

Input: arr = [10,11,12]

Output: 66

Constraints:

1 <= arr.length <= 100

1 <= arr[i] <= 1000

LeetCode Çözümleri – 617. Merge Two Binary Trees

LeetCode içerisinde bulunan “Merge Two Binary Trees” sorusunun açıklaması ve çözümü. Bu soruda size verilmiş olan iki adet binary tree içerisinde, aynı pozisyondaki node’ların değerlerini toplamanız isteniyor. Bunu istediğiniz binary tree içerisinde yapabilirsiniz.

► LeetCode 617. Merge Two Binary Trees : https://leetcode.com/problems/merge-two-binary-trees/

► Problem açıklaması:

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

Input:

Tree 1 Tree 2

1 2

/ \ / \

3 2 1 3

/ \ \

5 4 7

Output:

Merged tree:

3

/ \

4 5

/ \ \

5 4 7

Note: The merging process must start from the root nodes of both trees.

LeetCode Çözümleri – 925. Long Pressed Name

LeetCode içerisinde bulunan “Long Pressed Name” sorusunun açıklaması ve çözümü. Size verilen 2 adet string‘ten, biri diğerinin klavyede bastığı tuşun basılı kalma ihtimali ile oluşan hali olup olmadığı soruluyor. Tabi burada hiçbir tuşa basılı kalmama ihtimali de mevcut.

► LeetCode 925. Long Pressed Name: https://leetcode.com/problems/long-pressed-name/

► Problem açıklaması:

Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.

You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

Example 1:

Input: name = “alex”, typed = “aaleex”

Output: true

Explanation: ‘a’ and ‘e’ in ‘alex’ were long pressed.

Example 2:

Input: name = “saeed”, typed = “ssaaedd”

Output: false

Explanation: ‘e’ must have been pressed twice, but it wasn’t in the typed output.

Example 3:

Input: name = “leelee”, typed = “lleeelee”

Output: true

Example 4:

Input: name = “laiden”, typed = “laiden”

Output: true

Explanation: It’s not necessary to long press any character.

Constraints:

1 <= name.length <= 1000

1 <= typed.length <= 1000

name and typed contain only lowercase English letters.

LeetCode Çözümleri – 881. Boats to Save People

LeetCode içerisinde bulunan “Boats to Save People” sorusunun açıklaması ve çözümü. Her bir bot aynı anda en fazla 2 kişiyi taşıma koşulu ile, kişilerin ağırlığı ve her bir botun taşıma kapasitesi verildiği taktirde, tüm kişileri taşımak için en az kaç bota ihtiyacınız olduğu soruluyor.

► LeetCode 881. Boats to Save People: https://leetcode.com/problems/boats-to-save-people/

► Problem açıklaması:

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)

Example 1:

Input: people = [1,2], limit = 3

Output: 1

Explanation: 1 boat (1, 2)

Example 2:

Input: people = [3,2,2,1], limit = 3

Output: 3

Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5

Output: 4

Explanation: 4 boats (3), (3), (4), (5)

Note:

1 <= people.length <= 50000

1 <= people[i] <= limit <= 30000

LeetCode Çözümleri – 724. Find Pivot Index

LeetCode içerisinde bulunan “Find Pivot Index” sorusunun açıklaması ve çözümü. Pivot index, sağındaki ve solundaki elemanların toplamı eşit olan eleman anlamındadır. Size verilen bir tamsayı dizisinde, varsa pivot elemanının index’ini bulmanız isteniyor.

► LeetCode 724. Find Pivot Index: https://leetcode.com/problems/find-pivot-index/

► Problem açıklaması:

Given an array of integers nums, write a method that returns the “pivot” index of this array.

We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:

Input: nums = [1,7,3,6,5,6]

Output: 3

Explanation: The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. Also, 3 is the first index where this occurs.

Example 2:

Input: nums = [1,2,3]

Output: -1

Explanation: There is no index that satisfies the conditions in the problem statement.

Constraints:

The length of nums will be in the range [0, 10000].

Each element nums[i] will be an integer in the range [-1000, 1000].

LeetCode Çözümleri – 728. Self Dividing Numbers

LeetCode içerisinde bulunan “Self Dividing Numbers” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir sayının, içerdiği tüm rakamlara tam olarak bölünüp bölünmediğini (0 hariç) soruluyor.

► LeetCode 728. Self Dividing Numbers: https://leetcode.com/problems/self-dividing-numbers/

► Problem açıklaması:

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input: left = 1, right = 22

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note: The boundaries of each input argument are 1 <= left <= right <= 10000.

LeetCode Çözümleri – 226. Invert Binary Tree

LeetCode içerisinde bulunan “Invert Binary Tree” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘i tersine çevirmeniz isteniyor. Binary tree yapısı ile en sık karşılaşabileceğiniz soru tipi. Rekürsif yapı ile mantığını oturtursanız çok kolay anlaşılabiliyor.

✨ Binary tree nedir?

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

➡️ Binary Tree Data Structure: https://www.geeksforgeeks.org/binary-tree-data-structure/

➡️ Binary Trees: https://www.cmpe.boun.edu.tr/~akin/cmpe223/chap4.htm

➡️ Binary Trees by Nick Parlante: http://cslibrary.stanford.edu/110/BinaryTrees.html

✨ LeetCode 226. Invert Binary Tree: https://leetcode.com/problems/invert-binary-tree/

➡️ Problem açıklaması:

Invert a binary tree.

Example:

Input:

4

/ \

2 7

/ \ / \

1 3 6 9

Output:

4

/ \

7 2

/ \ / \

9 6 3 1

Trivia:

This problem was inspired by this original tweet (https://twitter.com/mxcl/status/608682016205344768) by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.