instruction_seed
stringlengths
29
40.5k
source
stringclasses
6 values
Please focus on the functionality of this problem and provide code in Visual Basic .NET: You are given a **0-indexed** binary string `s` and two integers `minJump` and `maxJump`. In the beginning, you are standing at index `0`, which is equal to `'0'`. You can move from index `i` to index `j` if the following condition...
dolphin
translate this code to Go: ```groovy def reachNumber(int target) { target = Math.abs(target) int step = 0 int sum = 0 while (sum < target) { step++ sum += step } while ((sum - target) % 2 != 0) { step++ sum += step } return step } ```
dolphin
Please focus on the functionality of this problem and provide code in Rust: Given two positive integers `num1` and `num2`, find the positive integer `x` such that: * `x` has the same number of set bits as `num2`, and * The value `x XOR num1` is **minimal**. Note that `XOR` is the bitwise XOR operation. Return _t...
dolphin
convert this code from ANSI C to Perl: ```c #include <stdio.h> #include <stdlib.h> /** * Note: The returned array must be malloced, assume caller calls free(). */ int* addToArrayForm(int* num, int numSize, int k, int* returnSize){ *returnSize = numSize + 5; // set returnSize to a safe value int* res = (int*)...
dolphin
Please explain the reasoning behind the following solution and provide code in Mojo: You are given a string `text`. We want to display `text` on a screen of width `w` and height `h`. You can choose any font size from array `fonts`, which contains the available font sizes **in ascending order**. You can use the `FontIn...
dolphin
Please write the following solution using Perl: Given a list of `dominoes`, `dominoes[i] = [a, b]` is **equivalent to** `dominoes[j] = [c, d]` if and only if either (`a == c` and `b == d`), or (`a == d` and `b == c`) - that is, one domino can be rotated to be equal to another domino. Return _the number of pairs_ `(i, ...
dolphin
Please explain the reasoning behind the following solution and provide code in Lisp: An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The b...
dolphin
Please focus on the algorithm of this problem and provide code in TypeScript: In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based ...
dolphin
Please write the following solution using PHP: **HTML entity parser** is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself. The special characters and their entities for HTML are: * **Quotation Mark:** the entity is `&quot;` and symbol character...
dolphin
Please explain the reasoning behind the following solution and provide code in Fortran: You are given three positive integers: `n`, `index`, and `maxSum`. You want to construct an array `nums` (**0-indexed**) that satisfies the following conditions: * `nums.length == n` * `nums[i]` is a **positive** integer where ...
dolphin
translate this code from Ruby to Perl: ```ruby def count_unique_substrings(s) n = s.length unique_substrings = Set.new for i in 0...n for j in (i+1)..n substring = s[i...j] freq = Array.new(10, 0) substring.chars.each do |c| freq[c.to_i] += 1 ...
dolphin
Please explain the following problem in detail and provide code in Julia: A string can be **abbreviated** by replacing any number of **non-adjacent** substrings with their lengths. For example, a string such as `"substitution "` could be abbreviated as (but not limited to): * `"s10n "` ( `"s ubstitutio n "`) * `"s...
dolphin
Please explain the following problem in detail and provide code in Erlang: You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. The **depth** of an integer is the number of lists that it is inside of. For example, the nest...
dolphin
translate this code from Ruby to Kotlin: ```ruby def min_cost_to_move_chips(position) even_count = 0 odd_count = 0 position.each do |pos| if pos % 2 == 0 even_count += 1 else odd_count += 1 end end return [even_count, odd_count].min end ```
dolphin
Please explain the following problem in detail and provide code in Groovy: Given a string `s`, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. **Example 1:** **Input:** s = "Let's take LeetCode contest" **Output:** "s'teL ekat edoCteeL tsetnoc" ...
dolphin
Please write the following solution using Lisp: Given an array of integers `nums` and an integer `k`, return _the number of **unique** k-diff pairs in the array_. A **k-diff** pair is an integer pair `(nums[i], nums[j])`, where the following are true: * `0 <= i, j < nums.length` * `i != j` * `nums[i] - nums[j] ...
dolphin
Please explain the following problem in detail and provide code in Object Pascal: Given a **0-indexed** integer array `nums`, return `true` _if it can be made **strictly increasing** after removing **exactly one** element, or_ `false` _otherwise. If the array is already strictly increasing, return_ `true`. The array `...
dolphin
Please explain the following problem in detail and provide code in Rust: Given a string `s`, find two **disjoint palindromic subsequences** of `s` such that the **product** of their lengths is **maximized**. The two subsequences are **disjoint** if they do not both pick a character at the same index. Return _the **max...
dolphin
Please focus on the efficiency of this problem and provide code in Swift: Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is stil...
dolphin
Please focus on the algorithm of this problem and provide code in Ruby: Given an array `nums` of positive integers. Your task is to select some subset of `nums`, multiply each element by an integer and add all these numbers. The array is said to be **good** if you can obtain a sum of `1` from the array by any possible ...
dolphin
Please write the following solution using Groovy: A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or `null`. Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) of the list. The deep copy should c...
dolphin
Please write the following solution using Go: Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings `words`. For example, if `words = [ "abc ", "xyz "]` and the stream added the four characters (one by one) `'a'`, `'x'`, `'y'`, and `'...
dolphin
convert this code from Dart to C#: ```dart int longestConsecutive(List<int> nums) { Set<int> elements = nums.toSet(); int longestSeq = 0; for (int num in nums) { if (!elements.contains(num - 1)) { int currentNum = num; int currentSeq = 1; while (elements.contains(currentNum + 1)) { ...
dolphin
port this code from Perl to Go: ```perl sub num_steps { my ($s) = @_; my $steps = 0; my $carry = 0; for my $i (reverse 1..length($s)-1) { my $bit = substr($s, $i, 1); if ($bit && $carry) { $carry = 1; $steps += 2; } elsif ($bit || $carry) { ...
dolphin
Please explain the following problem in detail and provide code in Perl: Given a binary array `nums` and an integer `k`, return _the maximum number of consecutive_ `1`_'s in the array if you can flip at most_ `k` `0`'s. **Example 1:** **Input:** nums = \[1,1,1,0,0,0,1,1,1,1,0\], k = 2 **Output:** 6 **Explanation:** \...
dolphin
translate this code to F#: ```smalltalk "helper function to check if a cell can be reached from (0, 0)" canReach := [:grid :row :col | ((row < 0) or: [col < 0] or: [row >= grid size or: [col >= grid at: 1 size]]) or: [grid at: row+1 at: col+1 = 0] ifTrue: [^ false]. (row = grid size and: [col = grid at: 1 size]...
dolphin
Please write the following solution using Swift: You are given a string `allowed` consisting of **distinct** characters and an array of strings `words`. A string is **consistent** if all characters in the string appear in the string `allowed`. Return _the number of **consistent** strings in the array_ `words`. **Exam...
dolphin
Please explain the following problem in detail and provide code in F#: In LeetCode Store, there are `n` items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. You are given an integer array `price` where `pri...
dolphin
Please focus on the efficiency of this problem and provide code in Go: Given an integer `n`, return _the number of ways you can write_ `n` _as the sum of consecutive positive integers._ **Example 1:** **Input:** n = 5 **Output:** 2 **Explanation:** 5 = 2 + 3 **Example 2:** **Input:** n = 9 **Output:** 3 **Explanati...
dolphin
Please explain the reasoning behind the following solution and provide code in Julia: Given the following details of a matrix with `n` columns and `2` rows : * The matrix is a binary matrix, which means each element in the matrix can be `0` or `1`. * The sum of elements of the 0-th(upper) row is given as `upper`. ...
dolphin
translate this code from Fortran to Clojure: ```fortran recursive function dfs(freq) result(sum) integer, dimension(26) :: freq integer :: i, sum = 0 do i = 1, 26 if (freq(i) > 0) then sum = sum + 1 freq(i) = freq(i) - 1 sum = sum + dfs(freq) freq(i) =...
dolphin
Please focus on the functionality of this problem and provide code in Rust: The **minimum absolute difference** of an array `a` is defined as the **minimum value** of `|a[i] - a[j]|`, where `0 <= i < j < a.length` and `a[i] != a[j]`. If all elements of `a` are the **same**, the minimum absolute difference is `-1`. * ...
dolphin
Please write the following solution using Fortran: You are given a **0-indexed** 1-dimensional (1D) integer array `original`, and two integers, `m` and `n`. You are tasked with creating a 2-dimensional (2D) array with `m` rows and `n` columns using **all** the elements from `original`. The elements from indices `0` to...
dolphin
port this code to Elixir: ```csharp public int PoorPigs(int buckets, int minutesToDie, int minutesToTest) { int pigs = 0; while(Math.Pow((minutesToTest / minutesToDie + 1), pigs) < buckets) { pigs++; } return pigs; } ```
dolphin
Please explain the reasoning behind the following solution and provide code in Lisp: Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_. The **first** node is considered **odd**, and the **second** node is **e...
dolphin
Please focus on the efficiency of this problem and provide code in Lisp: You are given a **0-indexed** integer array `nums`, where `nums[i]` is a digit between `0` and `9` (**inclusive**). The **triangular sum** of `nums` is the value of the only element present in `nums` after the following process terminates: 1. L...
dolphin
Please explain the reasoning behind the following solution and provide code in Groovy: Design a text editor with a cursor that can do the following: * **Add** text to where the cursor is. * **Delete** text from where the cursor is (simulating the backspace key). * **Move** the cursor either left or right. When ...
dolphin
Please explain the following problem in detail and provide code in Swift: You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-righ...
dolphin
Please explain the following problem in detail and provide code in Ruby: Given two integers `n` and `k`, return _the_ `kth` _lexicographically smallest integer in the range_ `[1, n]`. **Example 1:** **Input:** n = 13, k = 2 **Output:** 10 **Explanation:** The lexicographical order is \[1, 10, 11, 12, 13, 2, 3, 4, 5, ...
dolphin
convert this code to Visual Basic .NET: ```c int* searchRange(int* nums, int numsSize, int target, int* returnSize){ int* result = (int*)malloc(sizeof(int)*2); *returnSize = 2; result[0] = -1; result[1] = -1; int left = 0, right = numsSize-1, mid; while (left <= right) { mid = left + (ri...
dolphin
Please write the following solution using Groovy: You are given a binary array `nums` containing only the integers `0` and `1`. Return _the number of **subarrays** in nums that have **more**_ `1`'_s than_ `0`_'s. Since the answer may be very large, return it **modulo**_ `109 + 7`. A **subarray** is a contiguous sequen...
dolphin
Please explain the reasoning behind the following solution and provide code in Clojure: Write a function that reverses a string. The input string is given as an array of characters `s`. You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory. ...
dolphin
translate this code to Rust: ```python def findMaxLength(nums): max_length, zeroes, ones = 0, 0, 0 count_map = {0: -1} for i, num in enumerate(nums): if num == 0: zeroes += 1 else: ones += 1 if zeroes - ones not in count_map: count_map[zeroes - o...
dolphin
Please explain the reasoning behind the following solution and provide code in python: You are given an integer array `jobs`, where `jobs[i]` is the amount of time it takes to complete the `ith` job. There are `k` workers that you can assign jobs to. Each job should be assigned to **exactly** one worker. The **working...
dolphin
Please focus on the functionality of this problem and provide code in java: You are given an `m x n` integer matrix `matrix` with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer of the previous row. Given an integer `targ...
dolphin
Please write the following solution using Perl: You are given a **0-indexed** integer array `nums` of length `n`. The **sum** **score** of `nums` at an index `i` where `0 <= i < n` is the **maximum** of: * The sum of the **first** `i + 1` elements of `nums`. * The sum of the **last** `n - i` elements of `nums`. ...
dolphin
convert this code to python: ```elixir defmodule Solution do def min_cost_to_move_chips(position) do even_count = Enum.count(position, &(rem(&1, 2) == 0)) odd_count = Enum.count(position, &(rem(&1, 2) != 0)) :math.min(even_count, odd_count) end end ```
dolphin
convert this code to Lua: ```php /** * Definition for a binary tree node. * class TreeNode { * public $val = null; * public $left = null; * public $right = null; * public $parent = null; * function __construct($val = 0, $left = null, $right = null, $parent = null) { * $this->val = $v...
dolphin
Please explain the following problem in detail and provide code in Erlang: Given a 2D integer array `matrix`, return _the **transpose** of_ `matrix`. The **transpose** of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices. **Example 1:** **Input:** matrix = \[\[1,2,3...
dolphin
Please write the following solution using Mojo: You are given the `head` of a linked list and two integers `m` and `n`. Traverse the linked list and remove some nodes in the following way: * Start with the head as the current node. * Keep the first `m` nodes starting with the current node. * Remove the next `n`...
dolphin
Please explain the reasoning behind the following solution and provide code in Julia: Given a `file` and assume that you can only read the file using a given method `read4`, implement a method `read` to read `n` characters. Your method `read` may be **called multiple times**. **Method read4:** The API `read4` reads *...
dolphin
Please explain the following problem in detail and provide code in TypeScript: You are given an array of positive integers `price` where `price[i]` denotes the price of the `ith` candy and a positive integer `k`. The store sells baskets of `k` **distinct** candies. The **tastiness** of a candy basket is the smallest a...
dolphin
Please write the following solution using Swift: You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]` represent the start and the end of the `ith` interval and `intervals` is sorted in ascending order by `starti`. You are also given an interval `newInterval = [start, end...
dolphin
Please explain the following problem in detail and provide code in Julia: You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge. The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed e...
dolphin
Please write the following solution using SmallTalk: An **n-bit gray code sequence** is a sequence of `2n` integers where: * Every integer is in the **inclusive** range `[0, 2n - 1]`, * The first integer is `0`, * An integer appears **no more than once** in the sequence, * The binary representation of every pa...
dolphin
convert this code to Haskell: ```go func isDistinct(n int) bool { uniqueDigits := make(map[int]bool) for n > 0 { digit := n % 10 if _, ok := uniqueDigits[digit]; ok { return false } uniqueDigits[digit] = true n /= 10 } return true } func countSpecial(...
dolphin
Please explain the following problem in detail and provide code in Fortran: You are given an integer `n` denoting the number of cities in a country. The cities are numbered from `0` to `n - 1`. You are also given a 2D integer array `roads` where `roads[i] = [ai, bi]` denotes that there exists a **bidirectional** road ...
dolphin
translate this code to F#: ```python def split_and_minimize(num): a = num // 1000 b = (num % 1000) // 100 c = (num % 100) // 10 d = num % 10 return ((a * 10 + c) * 10 + b) * 10 + d ```
dolphin
Please explain the following problem in detail and provide code in Clojure: You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `pr...
dolphin
Please explain the reasoning behind the following solution and provide code in Clojure: A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfe...
dolphin
Please explain the reasoning behind the following solution and provide code in Mojo: On an infinite plane, a robot initially stands at `(0, 0)` and faces north. Note that: * The **north direction** is the positive direction of the y-axis. * The **south direction** is the negative direction of the y-axis. * The *...
dolphin
translate this code to c++: ```ruby def depth_sum(nested_list) depth_sum_helper(nested_list, 1) end def depth_sum_helper(nested_list, depth) nested_list.sum do |element| if element.is_a?(Integer) element * depth else depth_sum_helper(element, depth + 1) end end end ```
dolphin
Please explain the following problem in detail and provide code in c++: Given a **0-indexed** integer array `nums`, return `true` _if it can be made **strictly increasing** after removing **exactly one** element, or_ `false` _otherwise. If the array is already strictly increasing, return_ `true`. The array `nums` is *...
dolphin
port this code from Lisp to Ruby: ```lisp (defun time-to-buy-tickets (tickets k) (let ((time 0)) (loop while (> (nth k tickets) 0) do (loop for i below (length tickets) do (when (> (nth i tickets) 0) (decf (nth i tickets)) (incf time) (when (and (= i k) (= (nth k ticket...
dolphin
port this code to PHP: ```javascript function scheduleCourse(courses) { courses.sort((a, b) => a[1] - b[1]); const pq = new PriorityQueue((a, b) => b - a); let time = 0; for (const course of courses) { time += course[0]; pq.push(course[0]); if (time > course[1]) { ...
dolphin
Please explain the following problem in detail and provide code in Go: You are given a string `s`, where every **two** consecutive vertical bars `'|'` are grouped into a **pair**. In other words, the 1st and 2nd `'|'` make a pair, the 3rd and 4th `'|'` make a pair, and so forth. Return _the number of_ `'*'` _in_ `s`_,...
dolphin
port this code from c++ to python: ```cpp #include <iostream> #include <string> #include <stack> bool backspaceCompare(std::string s, std::string t) { std::stack<char> sStack, tStack; for (char c : s) { if (c != '#') { sStack.push(c); } else if (!sStack.empty()) { sStac...
dolphin
port this code to Perl: ```lua local LUPrefix = {} LUPrefix.__index = LUPrefix function LUPrefix:new(n) local obj = {} obj.max_prefix_length = 0 obj.next_video = 1 obj.uploaded = {} for i=1,n+1 do obj.uploaded[i] = false end setmetatable(obj, self) return obj end function LUPre...
dolphin
Please focus on the efficiency of this problem and provide code in Lua: Given a string `queryIP`, return `"IPv4 "` if IP is a valid IPv4 address, `"IPv6 "` if IP is a valid IPv6 address or `"Neither "` if IP is not a correct IP of any type. **A valid IPv4** address is an IP in the form `"x1.x2.x3.x4 "` where `0 <= xi ...
dolphin
Please explain the reasoning behind the following solution and provide code in Lua: Given two positive integers `num1` and `num2`, find the positive integer `x` such that: * `x` has the same number of set bits as `num2`, and * The value `x XOR num1` is **minimal**. Note that `XOR` is the bitwise XOR operation. R...
dolphin
port this code from Mojo to Kotlin: ```mojo class Task(id: int, description: string, due_date: int, tags: List<string>): var id = id var description = description var due_date = due_date var tags = tags var completed = false class TodoList(): var task_id = 1 var tasks = {} def addTask(...
dolphin
Please explain the reasoning behind the following solution and provide code in Swift: You are given an integer `n`, the number of teams in a tournament that has strange rules: * If the current number of teams is **even**, each team gets paired with another team. A total of `n / 2` matches are played, and `n / 2` tea...
dolphin
Please explain the reasoning behind the following solution and provide code in F#: You are given a **0-indexed** integer array `nums`. A pair of indices `(i, j)` is a **bad pair** if `i < j` and `j - i != nums[j] - nums[i]`. Return _the total number of **bad pairs** in_ `nums`. **Example 1:** **Input:** nums = \[4,1...
dolphin
Please explain the reasoning behind the following solution and provide code in Elixir: Given the root of a binary tree, find the largest subtree, which is also a Binary Search Tree (BST), where the largest means subtree has the largest number of nodes. A **Binary Search Tree (BST)** is a tree in which all the nodes fo...
dolphin
Please focus on the algorithm of this problem and provide code in Go: Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses. Every house can be warmed, as long as the house is within the heater's warm radius range. Given the positions of `h...
dolphin
Please explain the following problem in detail and provide code in PHP: Given a rectangle of size `n` x `m`, return _the minimum number of integer-sided squares that tile the rectangle_. **Example 1:** **Input:** n = 2, m = 3 **Output:** 3 **Explanation:** `3` squares are necessary to cover the rectangle. `2` (square...
dolphin
Please focus on the algorithm of this problem and provide code in java: You are given a **0-indexed** array `arr` consisting of `n` positive integers, and a positive integer `k`. The array `arr` is called **K-increasing** if `arr[i-k] <= arr[i]` holds for every index `i`, where `k <= i <= n-1`. * For example, `arr ...
dolphin
Please write the following solution using F#: Given the `head` of a singly linked list, return `true` _if it is a_ _palindrome_ _or_ `false` _otherwise_. **Example 1:** **Input:** head = \[1,2,2,1\] **Output:** true **Example 2:** **Input:** head = \[1,2\] **Output:** false **Constraints:** * The number of node...
dolphin
Please focus on the efficiency of this problem and provide code in Lisp: You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`. Return _the size of the largest **island** in_ `grid` _after applying this operation_. An **island** is a 4-directionally connected group of ...
dolphin
Please explain the following problem in detail and provide code in Scala: You are given an integer array `nums` of size `n`. You are asked to solve `n` queries for each integer `i` in the range `0 <= i < n`. To solve the `ith` query: 1. Find the **minimum value** in each possible subarray of size `i + 1` of the arra...
dolphin
Please write the following solution using ANSI C: A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, `"Hello World "`, `"HELLO "`, `"hello world hello world "` are all sentences. Words consist of **only** uppercase and lowercase English letters. Two sent...
dolphin
port this code from Object Pascal to Lua: ```pascal function maxScore(nums: array of integer): integer; var i: integer; result, prefixSum: integer; begin result := 0; prefixSum := 0; for i := 0 to Length(nums) - 1 do begin nums[i] := abs(nums[i]); end; Arrays.sort(nums); for i := 0 to Length(nums)...
dolphin
translate this code from c++ to R: ```cpp void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for (int i = 0; i < n / 2; i++) { for (int j = i; j < n - i - 1; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - j - 1][i]; matrix[n - j - 1][i] = mat...
dolphin
Please explain the reasoning behind the following solution and provide code in Lisp: You are given two positive integers `n` and `target`. An integer is considered **beautiful** if the sum of its digits is less than or equal to `target`. Return the _minimum **non-negative** integer_ `x` _such that_ `n + x` _is beauti...
dolphin
Please write the following solution using Objective-C: There are two types of persons: * The **good person**: The person who always tells the truth. * The **bad person**: The person who might tell the truth and might lie. You are given a **0-indexed** 2D integer array `statements` of size `n x n` that represents ...
dolphin
Please explain the reasoning behind the following solution and provide code in java: Given an `m x n` binary matrix `mat`, return _the length of the longest line of consecutive one in the matrix_. The line could be horizontal, vertical, diagonal, or anti-diagonal. **Example 1:** **Input:** mat = \[\[0,1,1,0\],\[0,1,...
dolphin
translate this code from Groovy to Object Pascal: ```groovy def ratioSubstringCount(String s, int num1, int num2) { int count0 = 0, count1 = 0 Map<Integer, Integer> frequency = [0: 1] int result = 0 s.each { char c -> if (c == '0') { count0++ } else { count1++ ...
dolphin
translate this code to Perl: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 100 #define MAX_LOGS 500 typedef struct Log { int id; char type[6]; int time; } Log; int stack[MAX_LOGS]; int top = -1; int* exclusiveTime(int n, char logs[MAX_LOGS][20], int logsSize, int* returnSi...
dolphin
port this code to Lua: ```lisp (defun interval-finding (intervals queries) (setq intervals (sort intervals #'(lambda (x y) (< (car x) (car y))))) (let ((ans '())) (dolist (q queries) (let ((min-size most-positive-fixnum)) (dolist (interval intervals) (when (<= (car interval) q (cdr inter...
dolphin
Please focus on the algorithm of this problem and provide code in Perl: You are given an integer array `target`. You have an integer array `initial` of the same size as `target` with all elements initially zeros. In one operation you can choose **any** subarray from `initial` and increment each value by one. Return _...
dolphin
Please write the following solution using Rust: You are given an array `nums` of positive integers. In one operation, you can choose **any** number from `nums` and reduce it to **exactly** half the number. (Note that you may choose this reduced number in future operations.) Return _the **minimum** number of operations...
dolphin
port this code to Fortran: ```kotlin fun findArray(pref: IntArray): IntArray { val arr = IntArray(pref.size) arr[0] = pref[0] for (i in 1 until pref.size) { arr[i] = pref[i] xor pref[i - 1] } return arr } ```
dolphin
Please write the following solution using Object Pascal: Given an integer `n`, return `true` _if_ `n` _has **exactly three positive divisors**. Otherwise, return_ `false`. An integer `m` is a **divisor** of `n` if there exists an integer `k` such that `n = k * m`. **Example 1:** **Input:** n = 2 **Output:** false **...
dolphin
Please explain the following problem in detail and provide code in Lua: Given a year `year` and a month `month`, return _the number of days of that month_. **Example 1:** **Input:** year = 1992, month = 7 **Output:** 31 **Example 2:** **Input:** year = 2000, month = 2 **Output:** 29 **Example 3:** **Input:** year...
dolphin
translate this code to ANSI C: ```scala def minOperationsToSatisfyConditions(a: String, b: String): Int = { var same = 0 var less = 0 var greater = 0 for (i <- 0 until a.length) { if (a.charAt(i) == b.charAt(i)) { same += 1 } else if (a.charAt(i) < b.charAt(i)) { less += 1 } else { ...
dolphin
Please explain the reasoning behind the following solution and provide code in Scala: You are given a 2D array of strings `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` means that `Ai / Bi = values[i]`. Determine if there exists a contradiction in the equations. Retu...
dolphin
Please explain the following problem in detail and provide code in Dart: You are given an integer array `nums` of length `n` where `nums` is a permutation of the integers in the range `[1, n]`. You are also given a 2D integer array `sequences` where `sequences[i]` is a subsequence of `nums`. Check if `nums` is the sho...
dolphin
Please explain the following problem in detail and provide code in Scheme: Given the `root` of an N-ary tree of unique values, and two nodes of the tree `p` and `q`. You should move the subtree of the node `p` to become a direct child of node `q`. If `p` is already a direct child of `q`, do not change anything. Node `...
dolphin
translate this code from python to Swift: ```python def maxScore(scores, edges): adj_list = [[] for _ in range(len(scores))] result = -1 for edge in edges: adj_list[edge[0]].append(edge[1]) adj_list[edge[1]].append(edge[0]) for edge in edges: for neighbor in adj_list[edge[0]]: ...
dolphin
Please explain the reasoning behind the following solution and provide code in Perl: Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You...
dolphin