Tag Archives: leetcode çözümleri

LeetCode Çözümleri – 985. Sum of Even Numbers After Queries

LeetCode içerisinde bulunan “Sum of Even Numbers After Queries” sorusunun açıklaması ve çözümü. Size verilen bir tam sayı dizisi ve hangi elemana ne kadar ekleneceğini belirten bir jagged array ile, bu ekleme işlemi sonunda oluşan tüm çift sayıları geri döndürmeniz isteniyor.

► LeetCode 985. Sum of Even Numbers After Queries: https://leetcode.com/problems/sum-of-even-numbers-after-queries/

► Problem açıklaması:

We have an array A of integers, and an array queries of queries.

For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A.

(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)

Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.

Example 1:

Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]

Output: [8,6,2,4]

Explanation:

At the beginning, the array is [1,2,3,4].

After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.

After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.

After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.

After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

LeetCode Çözümleri – 200. Number of Islands [Amazon Mülakat Sorusu]

LeetCode içerisinde bulunan “Number of Islands” sorusunun açıklaması ve çözümü. Size verilen m * n boyutundaki bir grid üzerinde, kaç adet ada olduğunu bulmanız isteniyor. “Ada” tanımı olarak, 4 tarafı su ile çevrili olan kısımlar şeklinde belirtilmiş.

DFS algoritması nedir?

https://en.wikipedia.org/wiki/Depth-first_search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

► LeetCode 200. Number of Islands: https://leetcode.com/problems/number-of-islands/

► Problem açıklaması:

Given an m x n 2d grid map of ‘1’s (land) and ‘0’s (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input: grid = [

[“1″,”1″,”1″,”1″,”0”],

[“1″,”1″,”0″,”1″,”0”],

[“1″,”1″,”0″,”0″,”0”],

[“0″,”0″,”0″,”0″,”0”]

]

Output: 1

Example 2:

Input: grid = [

[“1″,”1″,”0″,”0″,”0”],

[“1″,”1″,”0″,”0″,”0”],

[“0″,”0″,”1″,”0″,”0”],

[“0″,”0″,”0″,”1″,”1”]

]

Output: 3

LeetCode Çözümleri – 16. 3Sum Closest

LeetCode içerisinde bulunan “3Sum Closest” sorusunun açıklaması ve çözümü. Size verilen bir tam sayı dizisi ve “hedef” toplam için, sayı dizisi içerisindeki 3 sayının toplamı olarak bu “hedef” sayısına en yakın olanı bulmanız isteniyor.

► LeetCode 16. 3Sum Closest: https://leetcode.com/problems/3sum-closest/

► Problem açıklaması:

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example 1:

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

Output: 2

Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

LeetCode Çözümleri – 101. Symmetric Tree

LeetCode içerisinde bulunan “Symmetric Tree” sorusunun açıklaması ve çözümü. Size root’u verilen bir binary tree‘nin merkezine göre simetrik olup olmadığını bulmanız isteniyor.

► LeetCode 101. Symmetric Tree: https://leetcode.com/problems/symmetric-tree/

► Problem açıklaması:

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

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

Output: true

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

Output: false

Constraints:

The number of nodes in the tree is in the range [1, 1000].

-100 <= Node.val <= 100

Follow up: Could you solve it both recursively and iteratively?

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.