LeetCode Çözümleri – 1154. Day of the Year

LeetCode içerisinde bulunan “Day of the Year” sorusunun açıklaması ve çözümü. Bu soruda size verilen string‘in gösterdiği tarihin yılın kaçıncı günü olduğunu bulmanız isteniyor.

► LeetCode 1154. Day of the Year: https://leetcode.com/problems/day-of-the-year/

► Problem açıklaması: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

Example 1:

Input: date = “2019-01-09”

Output: 9

Explanation: Given date is the 9th day of the year in 2019.

Example 2: Input: date = “2019-02-10” Output: 41

Example 3: Input: date = “2003-03-01” Output: 60

Example 4: Input: date = “2004-03-01” Output: 61

Constraints:

date.length == 10

date[4] == date[7] == ‘-‘, and all other date[i]’s are digits

date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

LeetCode Çözümleri – 485. Max Consecutive Ones

LeetCode içerisinde bulunan “Max Consecutive Ones” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde (1 ve 0’lardan oluşan) yan yana olan 1’lerin en uzun hali soruluyor.

► LeetCode 228. 485. Max Consecutive Ones: https://leetcode.com/problems/max-consecutive-ones/

► Problem açıklaması:

Given a binary array nums, return the maximum number of consecutive 1’s in the array.

Example 1:

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

Output: 3

Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

Example 2:

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

Output: 2

Constraints:

1 <= nums.length <= 10^5

nums[i] is either 0 or 1.

HackerRank Çözümleri – Repeated String

HackerRank içerisinde bulunan “Repeated String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string ve n sayısı için, verilen string’i n uzunlukta olacak şekilde karakterlerini yazarak sonuç string’inde kaç tane ‘a’ karakteri olduğunu bulmanız isteniyor.

► HackerRank – Repeated String: https://www.hackerrank.com/challenges/repeated-string/problem

► Problem açıklaması:

There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter a’s in the first n letters of the infinite string.

Example

s = ‘abcac’

n = 10

The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.

Function Description

Complete the repeatedString function in the editor below.

repeatedString has the following parameter(s):

s: a string to repeat

n: the number of characters to consider

Returns

int: the frequency of a in the substring

Input Format

The first line contains a single string, s.

The second line contains an integer, n.

Sample Input

Sample Input 0

aba

10

Sample Output 0

7

Explanation 0

The first n = 10 letters of the infinite string are abaabaabaa. Because there are 7 a’s, we return 7.

Sample Input 1

a

1000000000000

Sample Output 1

1000000000000

Explanation 1

Because all of the first n = 1000000000000 letters of the infinite string are a, we return 1000000000000 .

LeetCode Çözümleri – 448. Find All Numbers Disappeared in an Array

LeetCode içerisinde bulunan “Find All Numbers Disappeared in an Array” sorusunun açıklaması ve çözümü. Bu soruda size [1, n] aralığındaki sayıları içerebilen bir tam sayı dizisinde, [1, n] aralığında olup ta bu dizide olmayan sayıları geriye döndürmeniz isteniyor.

► LeetCode 228. 448. Find All Numbers Disappeared in an Array: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

► Problem açıklaması:

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]

Output: [5,6]

Example 2:

Input: nums = [1,1]

Output: [2]

Constraints:

n == nums.length

1 <= n <= 10^5

1 <= nums[i] <= n

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

HackerRank Çözümleri – Cycle Detection

HackerRank içerisinde bulunan “Cycle Detection” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir linked list içerisinde döngüsel bir yapı olup olmadığını bulmanız isteniyor.

► HackerRank – Cycle Detection: https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem

► Problem açıklaması:

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.

Example

head refers to the list of nodes 1 — 2 — 3 — NULL

The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0.

head refers to the list of nodes 1 — 2 — 3 — 1 — NULL

There is a cycle where node 3 points back to node 1, so return 1.

Function Description

Complete the has_cycle function in the editor below.

It has the following parameter:

SinglyLinkedListNode pointer head: a reference to the head of the list

Returns

int:1 if there is a cycle or 0 if there is not

Note: If the list is empty, head will be null.

Input Format

The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases format will not be described for this question due to its complexity. Expand the section for the main function and review the code if you would like to figure out how to create a custom case.

LeetCode Çözümleri – 228. Summary Ranges

LeetCode içerisinde bulunan “Summary Ranges” sorusunun açıklaması ve çözümü. Bu soruda size verilen tam sayılarda, birbirini takip edenleri “aralık” olarak yazdırmanız isteniyor. Yani [0,1,2,4,5,7] için sizden [“0–2″,”4–5″,”7”] şeklinde bir liste isteniyor.

► LeetCode 228. Summary Ranges: https://leetcode.com/problems/summary-ranges/

► Problem açıklaması:

You are given a sorted unique integer array nums.

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

“a–b” if a != b

“a” if a == b

Example 1:

Input: nums = [0,1,2,4,5,7]

Output: [“0–2″,”4–5″,”7”]

Explanation: The ranges are:

[0,2] — “0–2”

[4,5] — “4–5”

[7,7] — “7”

Example 2:

Input: nums = [0,2,3,4,6,8,9]

Output: [“0″,”2–4″,”6″,”8–9”]

Explanation: The ranges are:

[0,0] — “0”

[2,4] — “2–4”

[6,6] — “6”

[8,9] — “8–9”

Example 3:

Input: nums = []

Output: []

Example 4:

Input: nums = [-1]

Output: [“-1”]

Example 5:

Input: nums = [0]

Output: [“0”]

Constraints:

0 lessEqual nums.length lessEqual 20

-2^31 <= nums[i] <= 2^31 – 1

All the values of nums are unique.

nums is sorted in ascending order.

LeetCode Çözümleri – 1184. Distance Between Bus Stops

LeetCode içerisinde bulunan “Distance Between Bus Stops” sorusunun açıklaması ve çözümü. Bu soruda size verilen başlangıç ve bitiş otobüs durak numaralarına göre (ki bu duraklar dairesel olarak dizilmişler), bunlar arasında gidebileceğiniz en kısa mesafeyi (saat yönünde veya tersinde) bulmanız isteniyor.

► LeetCode 1184. Distance Between Bus Stops: https://leetcode.com/problems/distance-between-bus-stops/

► Problem açıklaması:

A bus has n stops numbered from 0 to n – 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

The bus goes along both directions i.e. clockwise and counterclockwise. Return the shortest distance between the given start and destination stops.

Example 1:

Input: distance = [1,2,3,4], start = 0, destination = 1

Output: 1

Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1

Example 2:

Input: distance = [1,2,3,4], start = 0, destination = 2

Output: 3

Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.

Example 3:

Input: distance = [1,2,3,4], start = 0, destination = 3

Output: 4

Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.

Constraints:

1 <= n <= 10^4

distance.length == n

0 <= start, destination < n

0 <= distance[i] <= 10^4

HackerRank Çözümleri – Sherlock and Squares

HackerRank içerisinde bulunan “Sherlock and Squares” sorusunun açıklaması ve çözümü. Bu soruda verilen iki sayı arasındaki tam kare olan sayıların adetini bulmanız istenir.

► HackerRank – Sherlock and Squares: https://www.hackerrank.com/challenges/sherlock-and-squares/problem

► Problem açıklaması:

Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.

Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25

Example

a = 24

b = 49

There are three square integers in the range: 25, 36 and 49. Return 3.

Function Description

Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.

squares has the following parameter(s):

int a: the lower range boundary

int b: the upper range boundary

Returns

int: the number of square integers in the range.

Input Format

The first line contains q, the number of test cases. Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.

Sample Input

2

3 9

17 24

Sample Output

2

0

HackerRank Çözümleri – Viral Advertising

HackerRank içerisinde bulunan “Viral Advertising” sorusunun açıklaması ve çözümü. HackerLand Enterprise firması yeni bir reklam stratejisi üzerinde çalışıyor. İlk günde bu reklamı 5 kişiye gösteriyorlar sosyal medyada. Ve bu kişi sayısının yarısı 3’er arkadaşı ile paylaşıyor bu reklamı. Size verilen n gün sonra bu reklamı kaç kişi gördüğünü bulmanız isteniyor.

► HackerRank – Viral Advertising: https://www.hackerrank.com/challenges/strange-advertising/problem

► Problem açıklaması:

HackerLand Enterprise is adopting a new viral advertising strategy. When they launch a new product, they advertise it to exactly 5 people on social media.

On the first day, half of those 5 people (i.e., 5 / 2 = 2) like the advertisement and each shares it with 3 of their friends. At the beginning of the second day, (5 / 2 * 3 = 2 *3 = 6) people receive the advertisement.

Each day, floor(recipients / 2) of the recipients like the advertisement and will share it with 3 friends on the following day. Assuming nobody receives the advertisement twice, determine how many people have liked the ad by the end of a given day, beginning with launch day as day 1.

Example

n = 5.

Day Shared Liked Cumulative

1 5 2 2

2 6 3 5

3 9 4 9

4 12 6 15

5 18 9 24

The progression is shown above. The cumulative number of likes on the 5th day is 24.

Function Description

Complete the viralAdvertising function in the editor below.

viralAdvertising has the following parameter(s):

int n: the day number to report

Returns

int: the cumulative likes at that day

Input Format

A single integer, , the day number.

Constraints

Sample Input

3

Sample Output

9

LeetCode Çözümleri – 507. Perfect Number

LeetCode içerisinde bulunan “Perfect Number” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit tamsayının “perfect number”, yani kendisi hariç pozitif tam bölenlerinin toplamı kendine eşit bir sayı olup olmadığını bulmanız isteniyor.

► LeetCode 507. Perfect Number: https://leetcode.com/problems/perfect-number/

► Problem açıklaması:

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.

Given an integer n, return true if n is a perfect number, otherwise return false.

Example 1:

Input: num = 28

Output: true

Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28.

Example 2:

Input: num = 6

Output: true

Example 3:

Input: num = 496

Output: true

Example 4:

Input: num = 8128

Output: true

Example 5:

Input: num = 2

Output: false

Constraints:

1 <= num <= 10^8