[LeetCode] House Robber II

213. House Robber II

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Read more
[LeetCode] Find Substring With Given Hash Value

2156. Find Substring With Given Hash Value

The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:

  • hash(s, p, m) = (val(s[0]) p0 + val(s[1]) p1 + … + val(s[k-1]) * pk-1) mod m.
    Where val(s[i]) represents the index of s[i] in the alphabet from val(‘a’) = 1 to val(‘z’) = 26.

You are given a string s and the integers power, modulo, k, and hashValue. Return sub, the first substring of s of length k such that hash(sub, power, modulo) == hashValue.

The test cases will be generated such that an answer always exists.

A substring is a contiguous non-empty sequence of characters within a string.

Read more
[Google foobar] Level 3 Bomb, Baby!

Google foobar challenge level 3 - Bomb, Baby!

Read more
[Google foobar] Level 3 Fuel Injection Perfection

Google foobar challenge level 3 - Fuel Injection Perfection

Read more
[Google foobar] Level 3 Doomsday Fuel

Google foobar challenge level 3 - Doomsday Fuel

Read more
[Google foobar] Level 2 Ion Flux Relabeling

Google foobar challenge level 2 - Ion Flux Relabeling

Read more
[Google foobar] Level 2 Gearing Up for Destruction

Google foobar challenge level 2 - Gearing Up for Destruction

Read more
[Google foobar] Level 1 Minion Work Assignments

Google foobar challenge level 1 - Minion Work Assignments

Read more
[LeetCode] 24 Game

679. 24 Game

You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators [‘+’, ‘-‘, ‘*’, ‘/‘] and the parentheses ‘(‘ and ‘)’ to get the value 24.

You are restricted with the following rules:

  • The division operator ‘/‘ represents real division, not integer division.
  • For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
  • Every operation done is between two numbers. In particular, we cannot use ‘-‘ as a unary operator.
  • For example, if cards = [1, 1, 1, 1], the expression “-1 - 1 - 1 - 1” is not allowed.
  • You cannot concatenate numbers together
  • For example, if cards = [1, 2, 1, 2], the expression “12 + 12” is not valid.

Return true if you can get such expression that evaluates to 24, and false otherwise.

Read more
[LeetCode] Determine if Two Strings Are Close

1657. Determine if Two Strings Are Close

Two strings are considered close if you can attain one from the other using the following operations:

  • Operation 1: Swap any two existing characters.
  • For example, abcde -> aecdb
  • Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
  • For example, aacabb -> bbcbaa (all a’s turn into b’s, and all b’s turn into a’s)
    You can use the operations on either string as many times as necessary.

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

Read more