Tag Archives: leetcode türkçe

LeetCode Çözümleri – 198. House Robber

LeetCode içerisinde bulunan “House Robber” sorusunun açıklaması ve çözümü. Bu soruda profesyonel bir hırsız olduğunuzu düşünün. Size verilmiş evlerden, içerisindeki parayı soymak istiyorsunuz, fakat art arda olan evleri soyarsanız alarm çalışıyor ve polise haber veriyor. Bu kurala bağlı olarak, elde edebileceğiniz maksimum para miktarını bulmanız gerekiyor.

✨ LeetCode 198. House Robber: https://leetcode.com/problems/house-robber/

➡️ Problem açıklaması:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: nums = [1,2,3,1]

Output: 4

Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: nums = [2,7,9,3,1]

Output: 12

Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.

Constraints:

0 <= nums.length <= 100

0 <= nums[i] <= 400

LeetCode Çözümleri – 1207. Unique Number of Occurrences

LeetCode içerisinde bulunan “Unique Number of Occurrences” sorusunun açıklaması ve çözümü. Size verilen tamsayı dizisinde bulunan tüm sayılardan, adet olarak hepsinin birbirinden farklı olup olmadığı soruluyor.

✨ LeetCode 1207. Unique Number of Occurrences: https://leetcode.com/problems/unique-number-of-occurrences/

➡️ Problem açıklaması:

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

Example 1:

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

Output: true

Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

Input: arr = [1,2]

Output: false

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]

Output: true

Constraints:

1 <= arr.length <= 1000

-1000 <= arr[i] <= 1000

LeetCode Çözümleri – 860. Lemonade Change

LeetCode içerisinde bulunan “Lemonade Change” sorusunun açıklaması ve çözümü. Bu soruda bir limonata satıcısı olduğunuzu düşünün. Başlangıçta elinizde hiç para üstü yok ve sırada bazı müşteriler var. Sıradaki müşterilere yeterli miktarda para üstü verip veremeyeceğinizi bulmanız isteniyor.

✨ LeetCode 860. Lemonade Change: https://leetcode.com/problems/lemonade-change/

➡️ Problem açıklaması:

At a lemonade stand, each lemonade costs $5.

Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).

Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.

Note that you don’t have any change in hand at first.

Return true if and only if you can provide every customer with correct change.

Example 1:

Input: [5,5,5,10,20]

Output: true

Explanation:

From the first 3 customers, we collect three $5 bills in order.

From the fourth customer, we collect a $10 bill and give back a $5.

From the fifth customer, we give a $10 bill and a $5 bill.

Since all customers got correct change, we output true.

Example 2:

Input: [5,5,10]

Output: true

Example 3:

Input: [10,10]

Output: false

Example 4:

Input: [5,5,10,10,20]

Output: false

Explanation:

From the first two customers in order, we collect two $5 bills.

For the next two customers in order, we collect a $10 bill and give back a $5 bill.

For the last customer, we can’t give change of $15 back because we only have two $10 bills.

Since not every customer received correct change, the answer is false.

Note:

0 <= bills.length <= 10000

bills[i] will be either 5, 10, or 20.

LeetCode Çözümleri – 455. Assign Cookies

LeetCode içerisinde bulunan “Assign Cookies” sorusunun açıklaması ve çözümü. Bu soruda size verilen kurabiyeler ile, maksimum stayıda çocuğu nasıl doyurabileceğinize ait bir soru sorulmakta.

✨ LeetCode 455. Assign Cookies: https://leetcode.com/problems/assign-cookies/

➡️ Problem açıklaması:

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] biggerEqual g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Example 1:

Input: g = [1,2,3], s = [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1.

Example 2:

Input: g = [1,2], s = [1,2,3]

Output: 2

Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.

Constraints:

1 <= g.length <= 3 * 10^4

0 <= s.length <= 3 * 10^4

1 <= g[i], s[j] <= 2^31 – 1

LeetCode Çözümleri – 242. Valid Anagram

LeetCode içerisinde bulunan “Valid Anagram” sorusunun açıklaması ve çözümü. Verilen iki string‘ten, biri diğerinin anagramı olup olmadığı soruluyor.

✨ Anagram nedir? https://tr.wikipedia.org/wiki/Anagram

Anagram, bir sözcüğün veya sözcük grubunun harflerinin değişik düzenle başka bir sözcüğü veya sözcük grubunu oluşturmasıdır.

Türkçe bir anagram örneği: Bahriyeli = Harbiyeli

✨ LeetCode 242. Valid Anagram: https://leetcode.com/problems/valid-anagram/

➡️ Problem açıklaması:

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = “anagram”, t = “nagaram”

Output: true

Example 2:

Input: s = “rat”, t = “car”

Output: false

Note: You may assume the string contains only lowercase alphabets.

Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case?

LeetCode Çözümleri – 75. Sort Colors

LeetCode içerisinde bulunan “Sort Colors” sorusunun açıklaması ve çözümü. Bu soruda size karışık olarak verilen kırmızı, beyaz ve mavi renklerden (sırasıyla 0, 1 ve 2 ile ifade ediliyor), önce tüm kırmızılar, sonra tüm beyazlar ve en son tüm maviler olacak şekilde, ekstra bir alan kullanmadan sıralamanız isteniyor.

➡️ Quick sort (Hızlı sıralama) nedir?

✨ Wikipedia açıklaması: https://tr.wikipedia.org/wiki/H%C4%B1zl%C4%B1_s%C4%B1ralama

✨ Geeksforgeeks açıklaması: https://www.geeksforgeeks.org/quick-sort/

✨ Algoritmanın görsel açıklaması: http://sorting.at/ ve https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

🔥 LeetCode 75. Sort Colors: https://leetcode.com/problems/sort-colors/

➡️ Problem açıklaması:

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Follow up:

Could you solve this problem without using the library’s sort function?

Could you come up with a one-pass algorithm using only O(1) constant space?

Example 1:

Input: nums = [2,0,2,1,1,0]

Output: [0,0,1,1,2,2]

Example 2:

Input: nums = [2,0,1]

Output: [0,1,2]

Example 3:

Input: nums = [0]

Output: [0]

Example 4:

Input: nums = [1]

Output: [1]

Constraints: n == nums.length

1 <= n <= 300

nums[i] is 0, 1, or 2.

LeetCode Çözümleri – 290. Word Pattern

LeetCode içerisinde bulunan “Word Pattern” sorusunun açıklaması ve çözümü. Bu soruda size verilen iki adet string içerisinde, kelimeler ve harfler olarak aynı tasarıma sahip olup olmadığı soruluyor.

🔥 LeetCode 290. Word Pattern: https://leetcode.com/problems/word-pattern/

➡️ Problem açıklaması:

Given a pattern and a string s, find if s follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

Example 1:

Input: pattern = “abba”, s = “dog cat cat dog”

Output: true

Example 2:

Input: pattern = “abba”, s = “dog cat cat fish”

Output: false

Example 3:

Input: pattern = “aaaa”, s = “dog cat cat dog”

Output: false

Example 4:

Input: pattern = “abba”, s = “dog dog dog dog”

Output: false

Constraints:

1 lessEqual pattern.length lessEqual 300

pattern contains only lower-case English letters.

1 lessEqual s.length lessEqual 3000

s contains only lower-case English letters and spaces ‘ ‘.

s does not contain any leading or trailing spaces.

All the words in s are separated by a single space.

LeetCode Çözümleri – 434. Number of Segments in a String

LeetCode içerisinde bulunan “Number of Segments in a String” sorusunun açıklaması ve çözümü. Bu soruda size verilen string içerisinde, “segment” yani boşluk karakteri içermeyen karakterler bütünü sayısını bulmanız isteniyor.

🔥 LeetCode 434. Number of Segments in a String: https://leetcode.com/problems/number-of-segments-in-a-string/

➡️ Problem açıklaması:

You are given a string s, return the number of segments in the string.

A segment is defined to be a contiguous sequence of non-space characters.

Example 1:

Input: s = “Hello, my name is John”

Output: 5

Explanation: The five segments are [“Hello,”, “my”, “name”, “is”, “John”]

Example 2:

Input: s = “Hello”

Output: 1

Example 3:

Input: s = “love live! mu’sic forever”

Output: 4

Example 4:

Input: s = “”

Output: 0

Constraints:

0 <= s.length <= 300

s consists of lower-case and upper-case English letters, digits or one of the following characters “!@#$%^&*()_+-=’,.:”.

The only space character in s is ‘ ‘.

LeetCode Çözümleri – 674. Longest Continuous Increasing Subsequence

LeetCode içerisinde bulunan “Longest Continuous Increasing Subsequence” sorusunun açıklaması ve çözümü. Bu soruda sizden sürekli artışın olduğu en uzun alt diziyi bulmanız isteniyor. Bu tarz sorularda “Sliding Window” tekniği çok yaygın kullanılan bir tekniktir.

✨ Sliding Window tekniği nedir?

🟩 What is Sliding Window Algorithm? Examples?: https://stackoverflow.com/q/8269916/447156

🟩 An Introduction to Sliding Window Algorithms: https://levelup.gitconnected.com/an-introduction-to-sliding-window-algorithms-5533c4fe1cc7

🟩 How to Solve Sliding Window Problems: https://medium.com/outco/how-to-solve-sliding-window-problems-28d67601a66

🟩 Sliding Window Algorithm: https://www.baeldung.com/cs/sliding-window-algorithm

🔥 LeetCode 674. Longest Continuous Increasing Subsequence: https://leetcode.com/problems/longest-continuous-increasing-subsequence/

➡️ Problem açıklaması:

Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.

A continuous increasing subsequence is defined by two indices l and r (l less r) such that it is [nums[l], nums[l + 1], …, nums[r – 1], nums[r]] and for each l lessEqual i less r, nums[i] less nums[i + 1].

Example 1:

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

Output: 3

Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3. Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.

Example 2:

Input: nums = [2,2,2,2,2]

Output: 1

Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.

Constraints:

0 <= nums.length <= 10^4

-10^9 <= nums[i] <= 10^9

LeetCode Çözümleri – 48. Rotate Image

LeetCode içerisinde bulunan “Rotate Image” sorusunun açıklaması ve çözümü. Sizen matris olarak verilen bir resmi, saat yönünde 90 derece döndürmeniz isteniyor. Genel olarak burada ilk yapmamız gereken matrisi transpose etmemiz (https://en.wikipedia.org/wiki/Transpose), daha sonra da her satırı ters çevirmemiz bizim için yeterli oluyor.

Bu arada bunu in-place olarak, yani ek bir matris oluşturmadan, verilen matrisin içerisinde yapmanız gerekiyor.

Ek olarak, 12:31 ile 16:36 arası yaptığım hatayı bulmaya çalışıyorum, bu aralığı pas geçebilirsiniz.

🔥 LeetCode 48. Rotate Image: https://leetcode.com/problems/rotate-image/

➡️ Problem açıklaması:

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

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

Output: [[7,4,1],[8,5,2],[9,6,3]]

Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Example 3:

Input: matrix = [[1]]

Output: [[1]]

Example 4:

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

Output: [[3,1],[4,2]]

Constraints:

matrix.length == n

matrix[i].length == n

1 <= n <= 20

-1000 <= matrix[i][j] <= 1000