Jekyll2022-10-13T11:33:55+00:00https://yaeba.github.io/feed.xmlXtaycationMy personal blog. I write about software development, coding exercises, cool tips and tricks and notes for learning & development purposes.Xuanken Tayxuankentay@gmail.comAnagram Substrings2022-10-13T00:00:00+00:002022-10-13T00:00:00+00:00https://yaeba.github.io/exercises/anagram-substringsGiven two strings s0 and s1, return the number of substrings where s1 contains any anagram of s0.

Constraints

• n ≤ 100,000 where n is the length of s0
• m ≤ 100,000 where m is the length of s1

https://binarysearch.com/problems/Anagram-Substrings

## Examples

### Example 1

Input

• s0 = abc
• s1 = bcabxabc

Output

• answer = 3

Explanation

The substrings "bca", "cab" and "abc" of s0 are permutations of "abc".

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Separate People Given Dislike Relations2022-10-12T00:00:00+00:002022-10-12T00:00:00+00:00https://yaeba.github.io/exercises/separate-people-given-dislike-relationsYou are given an integer n and a two-dimensional list of integers enemies. Integer n means there’s n people labeled from [0, n - 1]. Each element in enemies contains [person_a, person_b] which means that person_a and person_b are enemies.

Return whether it’s possible to partition the n people into two groups such that no two people that are enemies are in the same group.

Constraints

• n ≤ 1,000
• m ≤ 10,000 where m is the length of enemies

https://binarysearch.com/problems/Separate-People-Given-Dislike-Relations

## Examples

### Example 1

Input

• n = 4
• enemies =
[[0,1],
[1,2]]


Output

• answer = True

Explanation

We can have these two groups [0, 2, 3] and .

### Example 2

Input

• n = 3
• enemies =
[[0,1],
[0,2],
[1,2]]


Output

• answer = False

Explanation

No matter how we split the two groups, there will be two people that are enemies in a group.

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Fractional Knapsack2022-10-10T00:00:00+00:002022-10-10T00:00:00+00:00https://yaeba.github.io/exercises/fractional-knapsackYou are given two lists of integers weights and values which have the same length and an integer capacity. weights[i] and values[i] represent the weight and value of the ith item.

Given that you can take at most capacity weights, and that you can take a fraction of an item’s weight with proportionate value, return the maximum amount of value you can get, rounded down to the nearest integer.

Constraints

• n ≤ 100,000 where n is the length of weights and values

https://binarysearch.com/problems/Fractional-Knapsack

## Examples

### Example 1

Input

• weights = [5, 6, 2]
• values = [100, 100, 1]
• capacity = 8

Output

• answer = 150

Explanation

The best we can do is:

• Take the item with 5 weight and take the 100 value, leaving us with 3 capacity
• Take half of the item with 6 weight and take half of the 100 value.

### Example 2

Input

• weights = [5, 6, 2]
• values = [100, 100, 1]
• capacity = 13

Output

• answer = 201

Explanation

We can take all 3 items in full

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Longest Arithmetic Subsequence with Difference Constraint2022-10-08T00:00:00+00:002022-10-08T00:00:00+00:00https://yaeba.github.io/exercises/longest-arithmetic-subsequence-with-difference-constraintGiven a list of integers nums and an integer diff, return the length of the longest arithmetic subsequence where the difference between each consecutive numbers in the subsequence is diff.

Constraints

• 0 ≤ n ≤ 100,000 where n is the length of nums

https://binarysearch.com/problems/Longest-Arithmetic-Subsequence-with-Difference-Constraint

## Examples

### Example 1

Input

• nums = [-2, 0, 3, 6, 1, 9]
• diff = 3

Output

• answer = 4

Explanation

We can pick the subsequence [0, 3, 6, 9].

### Example 2

Input

• nums = [9, 8, 7, 5, 3]
• diff = -2

Output

• answer = 4

Explanation

We can pick the subsequence [9, 7, 5, 3].

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Maximum Sum Rectangle with Condition2022-10-07T00:00:00+00:002022-10-07T00:00:00+00:00https://yaeba.github.io/exercises/maximum-sum-rectangle-with-conditionGiven a two-dimensional integer matrix matrix and an integer k, return the largest sum of a rectangle ≤ k.

Constraints

• n, m ≤ 100 where n and m are the number of rows and columns in matrix.

https://binarysearch.com/problems/Maximum-Sum-Rectangle-with-Condition

## Examples

### Example 1

Input

• matrix =
[[ 2,-2],
[ 3, 2]]

• k = 6

Output

• answer = 5

Explanation

We can take the rectangle [2, 3] to get sum of 5.

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Create Largest Number From a List2022-10-04T00:00:00+00:002022-10-04T00:00:00+00:00https://yaeba.github.io/exercises/create-largest-number-from-a-listGiven a list of integers nums, rearrange its order to form the largest possible integer and return it as a string.

Constraints

• n ≤ 1,000 where n is the length of nums
• nums[i] ≤ 1,000

https://binarysearch.com/problems/Create-Largest-Number-From-a-List

## Examples

### Example 1

Input

• nums = [10, 7, 76, 415]

Output

• answer = 77641510

### Example 2

Input

• nums = [961, 745, 331, 794, 923]

Output

• answer = 961923794745331

### Example 3

Input

• nums = [45, 14, 70, 67, 95]

Output

• answer = 9570674514

### Example 4

Input

• nums = [70, 5, 94, 18, 78]

Output

• answer = 947870518

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Remove Duplicate Numbers2022-10-01T00:00:00+00:002022-10-01T00:00:00+00:00https://yaeba.github.io/exercises/remove-duplicate-numbersGiven a list of integers nums, remove numbers that appear multiple times in the list, while maintaining order of the appearance in the original list.

It should use $$\mathcal{O}(k)$$ space where k is the number of unique integers.

Constraints

• n ≤ 100,000 where n is the length of nums

https://binarysearch.com/problems/Remove-Duplicate-Numbers

## Examples

### Example 1

Input

• nums = [1, 3, 5, 0, 3, 5, 8]

Output

• answer = [1, 0, 8]

Explanation

Only [1, 0, 8] are unique in the list and that’s the order they appear in.

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Longest Increasing Path2022-09-29T00:00:00+00:002022-09-29T00:00:00+00:00https://yaeba.github.io/exercises/longest-increasing-pathGiven a two-dimensional integer matrix, find the length of the longest strictly increasing path. You can move up, down, left, or right.

Constraints

• n, m ≤ 500 where n and m are the number of rows and columns in matrix

https://binarysearch.com/problems/Longest-Increasing-Path

## Examples

### Example 1

Input

• matrix =
[[1,3,5],
[0,4,6],
[2,2,9]]


Output

• answer = 6

Explanation

The longest path is [0, 1, 3, 5, 6, 9]

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Longest Sublist with K Distinct Numbers2022-09-28T00:00:00+00:002022-09-28T00:00:00+00:00https://yaeba.github.io/exercises/longest-sublist-with-k-distinct-numbersGiven an integer k and a list of integers nums, return the length of the longest sublist that contains at most k distinct integers.

Constraints

• 0 ≤ k ≤ n ≤ 100,000 where n is the length of nums

https://binarysearch.com/problems/Longest-Sublist-with-K-Distinct-Numbers

## Examples

### Example 1

Input

• k = 1
• nums = [0, 0, 0, 0, 0]

Output

• answer = 5

### Example 2

Input

• k = 2
• nums = [0, 1, 2, 1, 0]

Output

• answer = 3

Explanation

The longest substring with 2 distinct integers is [1,2,1], which has length of 3.

### Example 3

Input

• k = 1
• nums = [0, 1, 2, 3, 4]

Output

• answer = 1

## Solution

]]>
Xuanken Tayxuankentay@gmail.com
Anagram Partitioning2022-09-27T00:00:00+00:002022-09-27T00:00:00+00:00https://yaeba.github.io/exercises/anagram-partitioningYou are given two non-empty strings a and b that are the same length. Partition them into substrings such that each pair of a and b’s substring is the same size and they are anagrams of each other. Return the cut indexes such that it results in the largest number of cuts of a and b. Return the empty list if no result exists.

Constraints

• n ≤ 100,000 where n is the length of a and b

https://binarysearch.com/problems/Anagram-Partitioning

## Examples

### Example 1

Input

• a = study
• b = dusty

Output

• answer = [0, 4]

Explanation

We can cut the string into 2 partitions:

a = ["stud", "y"]

b = ["dust", "y"]

### Example 2

Input

• a = catdogwolf
• b = actgodflow

Output

• answer = [0, 2, 3, 6]

Explanation

We can cut the string into 4 partitions such that each string is an anagram of each other.

a = ["ca", "t", "dog", "wolf"]

b = ["ac", "t", "god", "flow"]

## Solution

]]>
Xuanken Tayxuankentay@gmail.com