HackerRank Çözümleri – CamelCase

HackerRank içerisinde bulunan “CamelCase” sorusunun açıklaması ve çözümü. Bu soruda size camelCase olarak yazılmış bir string’in içerisinde kaç adet kelime olduğunu bulmanız isteniyor.

► HackerRank – CamelCase: https://www.hackerrank.com/challenges/camelcase/problem

► Problem açıklaması:

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

– It is a concatenation of one or more words consisting of English letters.

– All letters in the first word are lowercase.

– For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

Example

s = oneTwoThree

There are 3 words in the string: ‘one’, ‘Two’, ‘Three’.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

string s: the string to analyze

Returns

int: the number of words in s.

Sample Input

saveChangesInTheEditor

Sample Output

5

Explanation

String s contains five words:

1. save

2. Changes

3. In

4. The

5. Editor

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

HackerRank Çözümleri – Fibonacci Modified

HackerRank içerisinde bulunan “Fibonacci Modified” sorusunun açıklaması ve çözümü. Bu soruda fibonacci serisinin değiştirilmiş hali olarak, bir önceki sayının kendisi değil de karesinin toplamı şeklinde çözüm bulmanız isteniyor.

► HackerRank – Fibonacci Modified: https://www.hackerrank.com/challenges/fibonacci-modified/problem

► Problem açıklaması:

Implement a modified Fibonacci sequence using the following definition:

t(i + 2) = t(i) + t(i + 2)^2

Given three integers, t1, t2, and n, compute and print the nth term of a modified Fibonacci sequence.

Function Description

Complete the fibonacciModified function in the editor below. It must return the nth number in the sequence.

fibonacciModified has the following parameter(s):

int t1: an integer

int t2: an integer

int n: the iteration to report

Returns

int: the nth number in the sequence

Note: The value of t[n] may far exceed the range of a 64-bit integer. Many submission languages have libraries that can handle such large results but, for those that don’t (e.g., C++), you will need to compensate for the size of the result.

Sample Input

0 1 5

Sample Output

5

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.

HackerRank Çözümleri – Extra Long Factorials

HackerRank içerisinde bulunan “Extra Long Factorials” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayının faktöriyelini bulmanız isteniyor. Bazı inputlar için faktöriyel 64-bit tam sayıyı aşabilir, bu yüzden daha büyük bir tam sayı tipi kullanmanız gerekiyor.

► HackerRank – Extra Long Factorials: https://www.hackerrank.com/challenges/extra-long-factorials/problem

► Problem açıklaması:

The factorial of the integer n, written n!, is defined as:

n! = n * (n – 1) * (n – 2) * … * 3 * 2 * 1

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

n: an integer

Note: Factorials of n biggerThan 20 can’t be stored even in a 64-bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

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.

HackerRank Çözümleri – Fair Rations

HackerRank içerisinde bulunan “Fair Rations” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde, aşağıdaki kurala istinaden kaç adet sayı eklemesi yapılarak tüm sayıların çift sayı olabileceği soruluyor.

► HackerRank – Fair Rations: https://www.hackerrank.com/challenges/fair-rations/problem

► Problem açıklaması:

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread. Your subjects are in a line, and some of them already have some loaves. Times are hard and your castle’s food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules:

1. Every time you give a loaf of bread to some person i, you must also give a loaf of bread to the person immediately in front of or behind them in the line (i.e., persons i + 1 or i – 1).

2. After all the bread is distributed, each person must have an even number of loaves.

Given the number of loaves already held by each citizen, find and print the minimum number of loaves you must distribute to satisfy the two rules above. If this is not possible, print NO.

Sample Input 0

5 2 3 4 5 6

Sample Output 0

4

Explanation 0

The initial distribution is (2, 3, 4, 5, 6). The requirements can be met as follows:

1. Give loaf of bread each to the second and third people so that the distribution becomes (2, 4, 5, 5, 6).

2. Give loaf of bread each to the third and fourth people so that the distribution becomes (2, 4, 6, 6, 6).

Each of the N subjects has an even number of loaves after 4 loaves were distributed.

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?

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