Tag Archives: leetcode çözümleri

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

LeetCode Çözümleri – 621. Task Scheduler [Facebook Mülakat Sorusu]

LeetCode içerisinde bulunan “Task Scheduler” sorusunun açıklaması ve çözümü. Sizen verilen görev listesi ve N sayısına istinaden, aynı task’lar arasında en az N kadar farklı task çalışması koşulu ile, en az kaç seferde bütün taskların çalıştırılabileceği soruluyor. LeetCode’a göre daha önce Facebook mülakatlarında sorulmuş sorulardan biriymiş.

🔥 LeetCode 621. Task Scheduler: https://leetcode.com/problems/task-scheduler/

➡️ Problem açıklaması:

Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.

However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.

Return the least number of units of times that the CPU will take to finish all the given tasks.

Example 1:

Input: tasks = [“A”,”A”,”A”,”B”,”B”,”B”], n = 2

Output: 8

Explanation: A — B — idle — A — B — idle — A — B

There is at least 2 units of time between any two same tasks.

Example 2:

Input: tasks = [“A”,”A”,”A”,”B”,”B”,”B”], n = 0

Output: 6

Explanation: On this case any permutation of size 6 would work since n = 0.

[“A”,”A”,”A”,”B”,”B”,”B”]

[“A”,”B”,”A”,”B”,”A”,”B”]

[“B”,”B”,”B”,”A”,”A”,”A”]

And so on.

Example 3:

Input: tasks = [“A”,”A”,”A”,”A”,”A”,”A”,”B”,”C”,”D”,”E”,”F”,”G”], n = 2

Output: 16

Explanation:

One possible solution is

A — B — C — A — D — E — A — F — G — A — idle — idle — A — idle — idle — A

Constraints:

1 <= task.length <= 10^4

tasks[i] is upper-case English letter.

The integer n is in the range [0, 100].

LeetCode Çözümleri – 720. Longest Word in Dictionary

LeetCode içerisinde bulunan “Longest Word in Dictionary” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string array içerisinde, kendisi hariç diğer kelimelerden birer harf alarak oluşturulabilecek en uzun kelimeyi bulmanız isteniyor. Eğer birden fazla varsa, alfabetik olarak önce gelen kelimeyi yazdırmanız isteniyor.

🔥 LeetCode 720. Longest Word in Dictionary: https://leetcode.com/problems/longest-word-in-dictionary/

➡️ Problem açıklaması:

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words.

If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.

Example 1:

Input: words = [“w”,”wo”,”wor”,”worl”, “world”]

Output: “world”

Explanation: The word “world” can be built one character at a time by “w”, “wo”, “wor”, and “worl”.

Example 2:

Input: words = [“a”, “banana”, “app”, “appl”, “ap”, “apply”, “apple”]

Output: “apple”

Explanation: Both “apply” and “apple” can be built from other words in the dictionary. However, “apple” is lexicographically smaller than “apply”.

Note:

All the strings in the input will only contain lowercase letters.

The length of words will be in the range [1, 1000].

The length of words[i] will be in the range [1, 30].