Tag Archives: leetcode çözümleri

LeetCode Çözümleri – 119. Pascal’s Triangle II

LeetCode içerisinde bulunan “Pascal’s Triangle II” sorusunun açıklaması ve çözümü. Size verilen bir satır numarasına istinaden (0 indeksli), o satıra tekabül eden pascal üçgeni satırını bir liste olarak döndürmeniz isteniyor.

► LeetCode 119. Pascal’s Triangle II: https://leetcode.com/problems/pascals-triangle-ii/

► Problem açıklaması:

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: rowIndex = 3

Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0

Output: [1]

Example 3:

Input: rowIndex = 1

Output: [1,1]

Constraints:

0 <= rowIndex <= 33

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

LeetCode Çözümleri – 11. Container With Most Water

LeetCode içerisinde bulunan “Container With Most Water” sorusunun açıklaması ve çözümü. Size verilen bir dizi içerisinde, bu dizinin her bir elemanı dikey uzunluğu ifade edecek şekilde, iki dikey uzunluk arasına koyabileceğiniz suyun maksimum miktarı soruluyor.

► LeetCode 11. Container With Most Water: https://leetcode.com/problems/container-with-most-water/

► Problem açıklaması:

Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

Input: height = [1,8,6,2,5,4,8,3,7]

Output: 49

Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = [1,1]

Output: 1

Example 3:

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

Output: 16

Example 4:

Input: height = [1,2,1]

Output: 2

LeetCode Çözümleri – 419. Battleships in a Board

LeetCode içerisinde bulunan “Battleships in a Board” sorusunun açıklaması ve çözümü. Size verilen bir grid üzerinde, soruda açıklanan kurallara göre, kaç tane savaş gemisi olduğu bulmanız isteniyor.

► LeetCode 419. Battleships in a Board: https://leetcode.com/problems/battleships-in-a-board/

► Problem açıklaması:

Given an 2D board, count how many battleships are in it. The battleships are represented with ‘X’s, empty slots are represented with ‘.’s. You may assume the following rules:

You receive a valid board, made of only battleships or empty slots.

Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.

At least one horizontal or vertical cell separates between two battleships – there are no adjacent battleships.

Example:

X..X

…X

…X

In the above board there are 2 battleships.

Invalid Example:

…X

XXXX

…X

This is an invalid board that you will not receive – as battleships will always have a cell separating between them.

Follow up:

Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?

LeetCode Çözümleri – 91. Decode Ways

LeetCode içerisinde bulunan “Decode Ways” sorusunun açıklaması ve çözümü. Size verilen string’in, her bir sayı alfabedeki harflerin sıralarına gelmesi ile, kaç farklı şekilde decode edilebileceği soruluyor.

► LeetCode 91. Decode Ways: https://leetcode.com/problems/decode-ways/

► Problem açıklaması:

A message containing letters from A-Z can be encoded into numbers using the following mapping:

‘A’ — “1”

‘B’ — “2”

‘Z’ — “26”

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:

“AAJF” with the grouping (1 1 10 6)

“KJF” with the grouping (11 10 6)

Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”.

Given a string s containing only digits, return the number of ways to decode it.

The answer is guaranteed to fit in a 32-bit integer.

Example 1:

Input: s = “12”

Output: 2

Explanation: “12” could be decoded as “AB” (1 2) or “L” (12).

Example 2:

Input: s = “226”

Output: 3

Explanation: “226” could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).

Example 3:

Input: s = “0”

Output: 0

Explanation: There is no character that is mapped to a number starting with 0. The only valid mappings with 0 are ‘J’ — “10” and ‘T’ — “20”, neither of which start with 0. Hence, there are no valid ways to decode this since all digits need to be mapped.

Example 4:

Input: s = “06”

Output: 0

Explanation: “06” cannot be mapped to “F” because of the leading zero (“6” is different from “06”).

Constraints:

1 <= s.length <= 100

s contains only digits and may contain leading zero(s).

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.