Tag Archives: leetcode çözümleri

LeetCode Çözümleri – 605. Can Place Flowers

LeetCode içerisinde bulunan “Can Place Flowers” sorusunun açıklaması ve çözümü. Bu soruda bir çiçekliği temsil bir tam sayı dizisinde, iki çiçeğin yan yana olmaması koşuluyla verilen çiçek sayısının hepsinin yerleştirilip yerleştirilemeyeceği soruluyor.

► LeetCode 605. Can Place Flowers: https://leetcode.com/problems/can-place-flowers/

► Problem açıklaması:

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0’s and 1’s, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1

Output: true

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2

Output: false

Constraints:

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

flowerbed[i] is 0 or 1.

There are no two adjacent flowers in flowerbed.

0 <= n <= flowerbed.length

LeetCode Çözümleri – 504. Base 7

LeetCode içerisinde bulunan “Base 7” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit integer değerin 7 tabanında gösterimini geriye döndürmeniz isteniyor.

► LeetCode 504. Base 7: https://leetcode.com/problems/base-7/

► Problem açıklaması:

Given an integer num, return a string of its base 7 representation.

Example 1:

Input: num = 100

Output: “202”

Example 2:

Input: num = -7

Output: “-10”

Constraints:

-10^7 lessEqual num lessEqual 10^7

LeetCode Çözümleri – 345. Reverse Vowels of a String

LeetCode içerisinde bulunan “Reverse Vowels of a String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string içerisindeki ünlü harfleri tersten yazdırmanız isteniyor.

► LeetCode 345. Reverse Vowels of a String: https://leetcode.com/problems/reverse-vowels-of-a-string/

► Problem açıklaması:

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.

Example 1:

Input: s = “hello”

Output: “holle”

Example 2:

Input: s = “leetcode”

Output: “leotcede”

Constraints:

1 <= s.length <= 3 * 105

s consist of printable ASCII characters.

LeetCode Çözümleri – 500. Keyboard Row

LeetCode içerisinde bulunan “Keyboard Row” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string dizisi içerisinde, Amerikan klavyesi üzerinde aynı satırdaki harflerden oluşan string’leri geri döndürmeniz isteniyor.

► LeetCode 500. Keyboard Row: https://leetcode.com/problems/keyboard-row/

► Problem açıklaması:

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

In the American keyboard:

the first row consists of the characters “qwertyuiop”,

the second row consists of the characters “asdfghjkl”, and

the third row consists of the characters “zxcvbnm”.

Example 1:

Input: words = [“Hello”,”Alaska”,”Dad”,”Peace”]

Output: [“Alaska”,”Dad”]

Example 2:

Input: words = [“omk”]

Output: []

Example 3:

Input: words = [“adsdf”,”sfd”]

Output: [“adsdf”,”sfd”]

Constraints: 1 <= words.length <= 20

1 <= words[i].length <= 100

words[i] consists of English letters (both lowercase and uppercase).

LeetCode Çözümleri – 342. Power of Four

LeetCode içerisinde bulunan “Power of Four” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit signed bir tam sayının 4’ün tam kuvveti olup olmadığını bulmanız isteniyor.

► LeetCode 342. Power of Four: https://leetcode.com/problems/power-of-four/

► Problem açıklaması:

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4^x.

Example 1:

Input: n = 16

Output: true

Example 2:

Input: n = 5

Output: false

Example 3:

Input: n = 1

Output: true

Constraints: -2^31 lessEqual n lessEqual 2^31 – 1

LeetCode Çözümleri – 190. Reverse Bits

LeetCode içerisinde bulunan “Reverse Bits” sorusunun açıklaması ve çözümü. Soruda size verilen 32-bit unsigned bir tam sayının bitlerini tersine çevirmeniz isteniyor.

► LeetCode 190. Reverse Bits: https://leetcode.com/problems/reverse-bits/

► Problem açıklaması:

Reverse bits of a given 32 bits unsigned integer.

Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Follow up:

If this function is called many times, how would you optimize it?

Example 1:

Input: n = 00000010100101000001111010011100

Output: 964176192 (00111001011110000010100101000000)

Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: n = 11111111111111111111111111111101

Output: 3221225471 (10111111111111111111111111111111)

Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

Constraints:

The input must be a binary string of length 32.

LeetCode Çözümleri – 695. Max Area of Island

LeetCode içerisinde bulunan “Max Area of Island” sorusunun açıklaması ve çözümü. Soruda size verilen 2 boyutlu bir grid içerisinde oluşan kara parçalarının (1) oluşturduğu en büyük ada alanını bulmanız isteniyor.

► LeetCode 191. Number of 1 Bits: https://leetcode.com/problems/number-of-1-bits/

► Problem açıklaması:

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],

[0,0,0,0,0,0,0,1,1,1,0,0,0],

[0,1,1,0,1,0,0,0,0,0,0,0,0],

[0,1,0,0,1,1,0,0,1,0,1,0,0],

[0,1,0,0,1,1,0,0,1,1,1,0,0],

[0,0,0,0,0,0,0,0,0,0,1,0,0],

[0,0,0,0,0,0,0,1,1,1,0,0,0],

[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.

LeetCode Çözümleri – 191. Number of 1 Bits

LeetCode içerisinde bulunan “Number of 1 Bits” sorusunun açıklaması ve çözümü. Soruda size verilen bir unsigned integer’ın binary gösteriminde kaç adet 1 olduğunu bulmanız isteniyor.

► LeetCode 191. Number of 1 Bits: https://leetcode.com/problems/number-of-1-bits/

► Problem açıklaması:

Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).

Note:

Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3, the input represents the signed integer. -3.

Example 1:

Input: n = 00000000000000000000000000001011

Output: 3

Explanation: The input binary string 00000000000000000000000000001011 has a total of three ‘1’ bits.

Example 2:

Input: n = 00000000000000000000000010000000

Output: 1

Explanation: The input binary string 00000000000000000000000010000000 has a total of one ‘1’ bit.

Example 3:

Input: n = 11111111111111111111111111111101

Output: 31

Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one ‘1’ bits.

Constraints: The input must be a binary string of length 32. Follow up: If this function is called many times, how would you optimize it?

LeetCode Çözümleri – 1288. Remove Covered Intervals

LeetCode içerisinde bulunan “Remove Covered Intervals” sorusunun açıklaması ve çözümü. Size verilen bir “aralık”lar dizisi içinde, birbirini kapsayan aralıklardan birini silmeniz isteniyor.

► LeetCode 1288. Remove Covered Intervals: https://leetcode.com/problems/remove-covered-intervals/

► Problem açıklaması:

Given a list of intervals, remove all intervals that are covered by another interval in the list.

Interval [a,b) is covered by interval [c,d) if and only if c lessEqual a and b lessEqual d.

After doing so, return the number of remaining intervals.

Example 1:

Input: intervals = [[1,4],[3,6],[2,8]]

Output: 2

Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.

Example 2:

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

Output: 1

Example 3:

Input: intervals = [[0,10],[5,12]]

Output: 2

Example 4:

Input: intervals = [[3,10],[4,10],[5,11]]

Output: 2

Example 5:

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

Output: 1

Constraints: All the intervals are unique.

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.