HackerRank Çözümleri – Minimum Loss

HackerRank içerisinde bulunan “Minimum Loss” sorusunun açıklaması ve çözümü. Bu soruda size ev fiyatları olarak verilen bir integer dizisi içerisinde, önce satın almak koşulu ile, satın alınıp sonrasında satış yapıldığında elde edebileceğiniz minimum zarar soruluyor.

► HackerRank – Minimum Loss: https://www.hackerrank.com/challenges/minimum-loss/problem

► Problem açıklaması:

Lauren has a chart of distinct projected prices for a house over the next several years. She must buy the house in one year and sell it in another, and she must do so at a loss. She wants to minimize her financial loss.

Example:

price = [20, 15, 8, 2, 12]

Her minimum loss is incurred by purchasing in year 2 at price[1] = 15 and reselling in year 5 at price[4] = 12. Return 15 – 12 = 3.

Function Description

Complete the minimumLoss function in the editor below.

minimumLoss has the following parameter(s):

int price[n]: home prices at each year

Returns int: the minimum loss possible

Sample Input 0

3

5 10 3

Sample Output 0

2

Sample Input

1

5 20 7 8 2 5

Sample Output 1

2

Sayı 2’nin tam kuvveti mi? [İteratif ve Bit manipülasyonu]

Bir sayının 2’nin bir tam kuvveti olup olmadığını nasıl bulabileceğinizi, hem iteratif yolla hem de bit manipülasyon yöntemleri ile anlatmaya çalıştım.

► İkinin kuvveti https://tr.wikipedia.org/wiki/%C4%B0kinin_kuvveti

Matematikte ikinin kuvveti, n bir tam sayı iken 2^n şeklinde gösterilebilen sayıdır. Bu üslü sayıda 2 taban, n ise üstür.

Sadece tam sayılar içinde düşünülecek olursa, n ancak pozitif bir değer alabilir. Bu durumda sonuç, 1, 2 ya da 2’nin belirli kere kendisiyle çarpılmasıyla elde edilen sayı olacaktır.

İki ikili sayı sisteminin de tabanını oluşturduğundan, ikinin kuvvetlerine bilgisayar biliminde sık rastlanır. Bu sistemde ikinin bir kuvveti her zaman onluk sayı sistemindeki onun kuvvetleri gibi 100…000 ya da 0.00…001 benzeri bir şekilde yazılacaktır.

Videodaki metodun text hali şurada: https://pastecode.io/s/RhOXm02CB7

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.

HackerRank Çözümleri – Find the Median

HackerRank içerisinde bulunan “Find the Median” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir seri içerisindeki medyan değerini, yani seriyi ortadan ikiye ayıran değeri bulmanız isteniyor.

► HackerRank – Find the Median: https://www.hackerrank.com/challenges/find-the-median/problem

► Problem açıklaması:

The median of a list of numbers is essentially its middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, find the median?

https://en.wikipedia.org/wiki/Median

Example

arr = [5, 3, 1, 2, 4]

The sorted array arr = [1, 2, 3, 4, 5]. The middle element and the median is 3.

Sample Input 0

7

0 1 2 4 6 5 3

Sample Output 0

3

The sorted arr = [0, 1, 2, 3, 4, 5, 6]. It’s middle element is at arr[3] = 3.

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

HackerRank Çözümleri – Time Complexity: Primality

HackerRank içerisinde bulunan “Time Complexity: Primality” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir sayının asal sayı olup olmadığını O(sqrt(n)) zamanında bulmanız isteniyor.

► HackerRank – Time Complexity: Primality: https://www.hackerrank.com/challenges/ctci-big-o/problem

► Problem açıklaması:

A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given p integers, determine the primality of each integer and return Prime or Not prime on a new line.

Note: If possible, try to come up with an O(sqrt(n)) primality algorithm, or see what sort of optimizations you can come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code.

Sample Input

STDIN Function

—– ——–

3 p = 3 (number of values to follow)

12 n = 12 (first number to check)

5 n = 5

7 n = 7

Sample Output

Not prime

Prime

Prime

Explanation

We check the following integers for primality:

n = 12 is divisible by numbers other than 1 and itself (i.e.: 2, 3, 4, 6).

n = 5 is only divisible 1 and itself.

n = 7 is only divisible 1 and itself.

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).

Sıfırdan C# Programlama Eğitim Seti – 6. Ders

►► Sıfırdan C# Programlama Eğitim Seti – 6. Ders

Bu yayınla C# programlama dilini temelinden anlatacağım eğitim setinin altıncı dersini tamamlamış olduk. Bu video serisi, genellikle hafta sonları Youtube üzerinde yapacağımız canlı yayınlar ile devam edecek.

6. Ders İçeriği;

0:00:00​ | Giriş

0:15:30 | ref locals ve ref returns kullanımı

0:30:20 | var kullanımı (c# 3.0)

0:39:59 | void kullanımı

0:42:16 | assignment expressions

0:51:49 | null coalescing operator (??)

0:56:07 | null coalescing assignment operator (??=) (c# 8.0)

1:02:35 | null-conditional operator (?.) (elvis operatörü)

1:15:27 | switch expression (c# 8.0)

Konularla ilgili faydalı linkler:

► Ref locals ve ref returns: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/ref-returns

► var anahtar sözcüğü: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

► void anahtar sözcüğü: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/void

► ?? ve ??= operatörleri: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

► ?. operatörü: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators–and-

► switch expression: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression

HackerRank Çözümleri – Bit Manipulation: Lonely Integer

HackerRank içerisinde bulunan “Bit Manipulation: Lonely Integer” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde, sadece bir elemanın tek adet, diğer tüm sayıların iki adet olduğu belirtilip, bu tek olan sayıyı bulmanız isteniyor.

► HackerRank – Bit Manipulation: Lonely Integer: https://www.hackerrank.com/challenges/ctci-lonely-integer/problem

► Problem açıklaması:

Consider an array of integers where all but one of the integers occur in pairs. In other words, every element occurs exactly twice except for one unique element. Find the unique element.

For example, given the array arr = [1, 1, 2, 3, 2], you would return 3.

Sample Input 0

1

1

Sample Output 0

1

Explanation 0

The array only contains a single 1, so we print 1 as our answer.

Sample Input 1

3

1 1 2

Sample Output 1

2

Explanation 1

We have two 1’s and one 2. We print 2, because that’s the only unique element in the array.

Sample Input 2

5

0 0 1 2 1

Sample Output 2

2

Explanation 2 We have two 0’s, two 1’s, and one 2. We print 2, because that’s the only unique element in the array.