john sokpo

All generations.

translateFri, 07 Jul 2023

class Solution: def maxConsecutiveAnswers(self, string: str, k: int) -> int: result = 0 j = 0 count1 = k for i in range(len(string)): if count1 == 0 and string[i] == "F": while string[j] != "F": j+=1 count1+=1 j+=1 if string[i] == "F": if count1 > 0: count1-=1 if i - j + 1 > result: result = i - j + 1 j = 0 count2 = k for i in range(len(string)): if count2 == 0 and string[i] == "T": while string[j] != "T": j+=1 count2+=1 j+=1 if string[i] == "T": if count2 > 0: count2-=1 if i - j + 1 > result: result = i - j + 1 return result

class Solution { function maxConsecutiveAnswers($string, $k) { $result = 0; $j = 0; $count1 = $k; for ($i = 0; $i < strlen($string); $i++) { if ($count1 == 0 && $string[$i] == "F") { while ($string[$j] != "F") { $j++; } $count1++; $j++; } if ($string[$i] == "F") { if ($count1 > 0) { $count1--; } } if ($i - $j + 1 > $result) { $result = $i - $j + 1; } } $j = 0; $count2 = $k; for ($i = 0; $i < strlen($string); $i++) { if ($count2 == 0 && $string[$i] == "T") {

PHP
View
generate functionFri, 07 Jul 2023

A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row). You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation: Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F'). Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

<?php function maxFlips( $answers, $k ) { $answers = str_split( $answers ); $answerTotal = count( $answers ); $maxFlips = 0; for ( $i = 0; $i < $answerTotal - 1; $i++ ) { $answerFlips = 0; $left = 0; for ( $j = $i; $j < $answerTotal; $j++ ) { if ( $answers[$i] === $answers[$j] ) { $answerFlips += $left; } else { $left++; } if ( $answerFlips <= $k && $j - $i + 1 > $maxFlips ) { $maxFlips = $j - $i + 1; } } } return $maxFlips; }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words; num /= 1000;

Java
View
generate functionFri, 07 Jul 2023

A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row). You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation: Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F'). Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

class Solution { /** * @param String $answerKey * @param Integer $k * @return Integer */ function maxConsecutive($answerKey, $k) { $result = ''; if ($k >= strlen($answerKey)) { return strlen($answerKey); } $answers = str_split($answerKey); $total = count($answers); $count = 0; for($i=0; $i<$total; $i++) { if ($i == 0) { $result .= $answers[$i]; } elseif ($answers[$i] == $answers[$i-1]) { $result .= $answers[$i]; } else { if ($k > 0) { $result .= $answers[$i - 1]; $k--; } else { $result .= $answers[$i];

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation.

class Solution { public String numberToWords(int num) { // check if num is within range if(num == 0) return "Zero"; if(num > 2147483647 || num < 0) return "Invalid Input"; String[] lessThanTwenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = {"", "Thousand", "Million", "Billion"}; String res = ""; int i = 0; while(num > 0){ if(num % 1000 != 0) res = helper(num%1000, lessThanTwenty, tens)

Java
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function max_consecutive_answers(array $answerKey, int $k): int { // TODO: Write your code here $left = 0; $right = 0; $numOfTrue = 0; $numOfFalse = 0; $max = 0; $moreChanges = function ($numOfTrue, $numOfFalse, $k) { return $numOfTrue > $k && $numOfFalse > $k; }; while ($right < count($answerKey)) { if ($answerKey[$right] === 'T') { $numOfTrue++; } if ($answerKey[$right] === 'F') { $numOfFalse++; } while ($moreChanges($numOfTrue, $numOfFalse, $k)) { if ($answerKey[$left] === 'T') { $numOfTrue--; } if ($answerKey[$left] === 'F') { $numOfFalse--;

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words;

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { if (num == 0) return "Zero"; String[] units = {"", "Thousand ", "Million ", "Billion "}; String res = ""; for (int i = 0; num > 0; i++) { String cur = ""; int tmp = num % 1000; if (tmp >= 100) cur += helper(tmp / 100) + "Hundred "; tmp %= 100; if (tmp >= 20) cur += helper(tmp / 10 * 10); tmp %= 10; if (tmp > 0) cur += helper(tmp); if (cur.length() > 0) res = cur + units[i] + res; num /= 1000; } res = res.trim(); return res; } private String helper(int n) { if (n == 0) return ""; String[] nums = {"", "One ", "Two ", "Three ", "Four ", "Five ",

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { String[] numbers = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; String[] tens = {"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"}; String[] thousands = {"","Thousand","Million","Billion"}; public String numberToWords(int num) { if(num == 0) return "Zero"; String res = ""; int count = 0; while(num > 0 ){ if(num % 1000 != 0){ res = helper(num % 1000) + thousands[count] + " " + res; } num = num / 1000; count++; } return res.trim(); } String helper(int num){ if(num == 0) return ""; if(

Java
View
generate functionThu, 06 Jul 2023

Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake that is full of water, there will be a flood. Your goal is to avoid floods in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.

function avoidFlood($rains) { $res = array_fill(0, count($rains), -1); $map = []; $zeros = []; foreach($rains as $i => $r){ if($r){ if(isset($map[$r])){ $res = []; break; }else{ $map[$r] = $i; } }else{ $zeros[] = $i; } } if(!$res) return $res; foreach($rains as $i => $r){ if($r){ $res[$i] = $r; if(!$zeros) return []; while($zeros && $zeros[0] < $map[$r]){ array_shift($zeros); } if(!$zeros) return []; $res[array_shift($zeros)] = $r; } }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public class Solution { public String numberToWords(int num) { if (num == 0) return "Zero"; String res = ""; int count = 0; while (num > 0) { int digit = num % 1000; num /= 1000; String tmp = helper(digit); res = tmp + " " + units[count] + " " + res; count++; } return res.trim(); } private String helper(int num) { if (num == 0) return ""; String res = ""; if (num / 100 > 0) { res = digits[num / 100] + " Hundred "; num %= 100; } if (num / 10 > 1) { res += tens[num / 10] + " "; num %= 10; } else if (num / 10 == 1) { res += teens[num % 10] + " ";

Java
View
translateFri, 07 Jul 2023

class Solution { // Binary Search + Sliding Window fixed public int maxConsecutiveAnswers(String answerKey, int k) { int start = 1 ; int end = answerKey.length(); int max_length = 0 ; while(start <= end) { int mid = start+(end-start)/2 ; if(isMax(answerKey , k , mid)) { max_length = mid ; start = mid+1 ; }else { end = mid-1 ; } } return max_length ; } public boolean isMax(String answerKey , int k , int max_val) { int T_count = 0 ; int F_count = 0 ; int i = 0 ; int j = 0 ; while(j < answerKey.length()) { if(answerKey.charAt(j) == 'T') { T_count++ ; }else { F_count++ ; } if(j-i+1 == max_val) { if(Math.max(T_count, F_count)+k >= max_val) { return true ; } if(answerKey.charAt(i) == 'T') { T_count-- ; }else { F_count-- ; } i++ ; } j++ ; } return false ; } }

class Solution { /** * @param Integer[] $nums * @return NULL */ function moveZeroes(&$nums) { $count = 0; for($i = 0;$i<count($nums);$i++) { if($nums[$i] != 0) { $nums[$count++] = $nums[$i]; } } while($count < count($nums)) { $nums[$count++] = 0; } } }

PHP
View
generate functionThu, 06 Jul 2023

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith customer. When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input. Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.

$arrival = [1,3,3,5,7]; $time = [2,4,1,3,3];

PHP
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function add($a, $b) { return $a + $b; }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { if (num == 0) return "Zero"; StringBuilder sb = new StringBuilder(); String[] one = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] ten = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] units = {"Zero", "Thousand", "Million", "Billion", "Trillion"}; int i = 0; while (num > 0) { if (num % 1000 != 0) { sb.insert(0, units[i] + " "); numToWordsHelper(sb, one, ten, num % 1000);

Java
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

class Solution { /** * @param Integer[] $nums * @param Integer $target * @return Integer[] */ function twoSum($nums, $target) { $hash = []; foreach ($nums as $i => $value) { $complement = $target - $value; if (isset($hash[$complement])) { return [$hash[$complement], $i]; } $hash[$value] = $i; } } }

PHP
View
translateFri, 07 Jul 2023

class Solution: def maxConsecutiveAnswers(self, string: str, k: int) -> int: result = 0 j = 0 count1 = k for i in range(len(string)): if count1 == 0 and string[i] == "F": while string[j] != "F": j+=1 count1+=1 j+=1 if string[i] == "F": if count1 > 0: count1-=1 if i - j + 1 > result: result = i - j + 1 j = 0 count2 = k for i in range(len(string)): if count2 == 0 and string[i] == "T": while string[j] != "T": j+=1 count2+=1 j+=1 if string[i] == "T": if count2 > 0: count2-=1 if i - j + 1 > result: result = i - j + 1 return result

<?php $num = 10; echo $num; ?>

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public class Solution { public String numberToWords(int num) { StringBuilder sb = new StringBuilder(); if(num == 0) return "Zero"; String[] underTwenty = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = new String[]{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = new String[]{"", "Thousand", "Million", "Billion"}; int i = 0; while(num > 0) { if(num % 1000 != 0) { sb.insert(0, thousands[i] + " "); helper(num % 1000, underTwenty, tens, sb);

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { String res = ""; String[] one = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] ten = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] eleven = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] hundred = {"Hundred"}; String[] thousand = {"Thousand"}; String[] million = {"Million"}; String[] billion = {"Billion"}; int one1 = num%10; int ten1 = num%100/10; int hun1 = num%1000/100; int thou1 = num%1000000/1000; int mil1 = num%

Java
View
generate functionFri, 07 Jul 2023

A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row). You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation: Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F'). Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

function testConfusion(string $answerKey, int $k): int { $answerKey = str_split($answerKey); $n = count($answerKey); $ans = 0; $c = 0; for($i=0;$i<$n;$i++){ if($answerKey[$i] == $answerKey[$i+1]){ $c++; }else{ if($c >= $k){ $ans = max($ans,$c+1); } $c = 0; } } if($c >= $k){ $ans = max($ans,$c+1); } return $ans; }

PHP
View
generate functionThu, 06 Jul 2023

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith customer. When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input. Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.

function restaurant($customers) { $ordered = []; $wait_time = 0; foreach ($customers as $order) { $ordered[$order[0]] = $order[1]; } ksort($ordered); $total_time = 0; while (count($ordered) > 0) { $start = array_keys($ordered)[0]; $time = array_shift($ordered); $wait_time += $total_time - $start; $total_time += $time; } return $wait_time / count($customers); }

PHP
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

$arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) unset($value); // break the reference with the last element

PHP
View
translateFri, 07 Jul 2023

class Solution { // Binary Search + Sliding Window fixed public int maxConsecutiveAnswers(String answerKey, int k) { int start = 1 ; int end = answerKey.length(); int max_length = 0 ; while(start <= end) { int mid = start+(end-start)/2 ; if(isMax(answerKey , k , mid)) { max_length = mid ; start = mid+1 ; }else { end = mid-1 ; } } return max_length ; } public boolean isMax(String answerKey , int k , int max_val) { int T_count = 0 ; int F_count = 0 ; int i = 0 ; int j = 0 ; while(j < answerKey.length()) { if(answerKey.charAt(j) == 'T') { T_count++ ; }else { F_count++ ; } if(j-i+1 == max_val) { if(Math.max(T_count, F_count)+k >= max_val) { return true ; } if(answerKey.charAt(i) == 'T') { T_count-- ; }else { F_count-- ; } i++ ; } j++ ; } return false ; } }

class Solution { // Binary Search + Sliding Window fixed function maxConsecutiveAnswers($answerKey, $k) { $start = 1; $end = strlen($answerKey); $max_length = 0; while ($start <= $end) { $mid = $start+($end-$start)/2; if($this->isMax($answerKey , $k , $mid)) { $max_length = $mid; $start = $mid+1; }else { $end = $mid-1; } } return $max_length ; } function isMax($answerKey , $k , $max_val) { $T_count = 0; $F_count = 0; $i = 0; $j = 0; while ($j < strlen($answerKey)) { if($answerKey[$j] == 'T') { $T_count++; }else { $F_

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public String numberToWords(int num) { if (num == 0) return "Zero"; String res = ""; String[] units = new String[]{"", "Thousand", "Million", "Billion"}; int i = 0; while (num > 0) { if (num % 1000 != 0) { res = helper(num % 1000) + units[i] + " " + res; } num /= 1000; i++; } return res.trim(); } private final String[] lessThan20 = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] tens = new String[]{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",

Java
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function add($a, $b) { return $a + $b; }

PHP
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function add($a, $b) { return $a + $b; }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation.

Script /** * @param {number} num * @return {string} */ var numberToWords = function(num) { if (num === 0) { return 'Zero'; } const units = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']; const tens = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']; const thousands = ['', ' Thousand ', ' Million ', ' Billion ']; let result; for (let i = 0; i < thousands.length; i++) { if (num % 1000 !== 0) { result = helper(num % 1000) + thousands[i] + (result || ''); } num = parseInt(num

Java
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function add(a, b): int { return a + b; }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation.

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words;

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { if(num == 0) return "Zero"; String[] below20 = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = {"Billion", "Million", "Thousand", ""}; int i = 0; String words = ""; while(num > 0) { if(num % 1000 != 0) { words = helper(num % 1000) + thousands[i] + " " + words; } num = num / 1000; i++; }

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public class Solution { public String numberToWords(int num) { String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] thousands = {"", "Thousand", "Million", "Billion"}; StringBuilder sb = new StringBuilder(); int i = 0; while (num > 0) { if (num % 1000 != 0) { sb.insert(0, helper(num % 1000, ones, tens) + thousands[i] + " "); } num /= 1000; i++; } return sb.length() == 0 ?

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words; num /= 1000;

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words; num /

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { if (num == 0) return "Zero"; String[] unit = {"", "Thousand", "Million", "Billion"}; int i = 0; String res = ""; while (num > 0) { if (num % 1000 != 0) { res = helper(num % 1000) + unit[i] + " " + res; } num /= 1000; i++; } return res.trim(); } private String helper(int num){ String[] lessThan20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] ty = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Hello world!"); System.out.println(numberToWords(123)); }//end main public static String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) + THOUSANDS[i] + " " + words; num /= 1000; i++; } return words.trim(); } private final static String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "

Java
View
translateFri, 07 Jul 2023

var maxConsecutiveAnswers = function(answerKey, k) { let [left, right, numOfTrue, numOfFalse, max] = new Array(5).fill(0); const moreChanges = () => numOfTrue > k && numOfFalse > k; while (right < answerKey.length) { if(answerKey[right] === 'T') numOfTrue++; if(answerKey[right] === 'F') numOfFalse++; while(moreChanges()) { if(answerKey[left] === 'T') numOfTrue--; if(answerKey[left] === 'F') numOfFalse--; left++; } max = Math.max(max, right - left + 1); right++; } return max; };

function add($a, $b) { return $a + $b; }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { String[] belowTen= new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] belowTwenty= new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] belowHundred= new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public String numberToWords(int num) { if (num == 0) return "Zero"; return helper(num); } public String helper(int num) { String result = new String(); if (num < 10) result = belowTen[num]; else if (num < 20) result = belowTwenty[num -10]; else if (num < 100) result = belowH

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { StringBuilder builder = new StringBuilder(); if (num == 0) { return "Zero"; } String[] unitsName = {"", "Thousand", "Million", "Billion"}; int unitIndex = 0; while (num > 0) { int temp = num % 1000; if (temp != 0) { builder.insert(0, unitsName[unitIndex] + " "); } builder.insert(0, convertToWords(temp)); num = num / 1000; unitIndex++; } return builder.toString().trim(); } /** * Convert 3 digit number to english words * @param num * @return */ private String convertToWords(int num) { StringBuilder builder = new StringBuilder(); String[] numNames = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "

Java
View
translateFri, 07 Jul 2023

class Solution: def maxConsecutiveAnswers(self, string: str, k: int) -> int: result = 0 j = 0 count1 = k for i in range(len(string)): if count1 == 0 and string[i] == "F": while string[j] != "F": j+=1 count1+=1 j+=1 if string[i] == "F": if count1 > 0: count1-=1 if i - j + 1 > result: result = i - j + 1 j = 0 count2 = k for i in range(len(string)): if count2 == 0 and string[i] == "T": while string[j] != "T": j+=1 count2+=1 j+=1 if string[i] == "T": if count2 > 0: count2-=1 if i - j + 1 > result: result = i - j + 1 return result

<?php $str = "ABCD"; $n = strlen($str); printAllKLength($str, $n, 3); function printAllKLength($str, $n, $k) { // Base case: k is 0, // print prefix if ($k == 0) { echo $str . "\n"; return; } // One by one add all characters // from set and recursively // call for k equals to k-1 for ($i = 0; $i < $n; ++$i) { // Next character of input added $newstr = substr($str, 0, $i) . substr($str, $i + 1); // k is decreased, because // we have added a new character printAllKLength($newstr, $n - 1, $k - 1); } } ?>

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public class Solution { public static String numberToWords(int num) { if (num == 0) return "Zero"; StringBuilder builder = new StringBuilder(); String[] digits = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] units = {"", "Thousand", "Million", "Billion"}; int unitIndex = 0; while (num > 0) { int digit = num % 1000; if (digit != 0) { if (unitIndex > 0) builder.insert(0, units[unitIndex] + " "); int ten =

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public String numberToWords(int num) { if (num == 0) return "Zero"; return helper(num); } public String helper(int num) { String[] units = new String[]{"", "Thousand", "Million", "Billion"}; String[] belowTwenty = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tens = new String[]{"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String res = ""; for(int i=0;num>0;i++){ if(num%1000!=0){ res = helper(num%1000) + units[i] + " " + res;

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

public class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words; num /= 1000;

Java
View
generate functionFri, 07 Jul 2023

A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row). You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation: Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F'). Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

class Solution { /** * @param String $answerKey * @param Integer $k * @return Integer */ function maxConsecutiveSameAnswers($answerKey, $k) { $len = strlen($answerKey); $res = 0; for($i = 0; $i < $len; $i++){ $j = 0; while($i + $j < $len && $answerKey[$i + $j] == $answerKey[$i]){ $j++; } $res = max($res, min($j, $k)); } return $res; } }

PHP
View
translateFri, 07 Jul 2023

class Solution: def maxConsecutiveAnswers(self, string: str, k: int) -> int: result = 0 j = 0 count1 = k for i in range(len(string)): if count1 == 0 and string[i] == "F": while string[j] != "F": j+=1 count1+=1 j+=1 if string[i] == "F": if count1 > 0: count1-=1 if i - j + 1 > result: result = i - j + 1 j = 0 count2 = k for i in range(len(string)): if count2 == 0 and string[i] == "T": while string[j] != "T": j+=1 count2+=1 j+=1 if string[i] == "T": if count2 > 0: count2-=1 if i - j + 1 > result: result = i - j + 1 return result

public function getMaxConsecutiveAnswers(string $string, int $k): int { $result = 0; $j = 0; $count1 = $k; for ($i = 0; $i < strlen($string); $i++) { if ($count1 == 0 && $string[$i] == 'F') { while ($string[$j] != 'F') { $j++; } $count1++; $j++; } if ($string[$i] == 'F') { if ($count1 > 0) { $count1--; } } if ($i - $j + 1 > $result) { $result = $i - $j + 1; } } $j = 0; $count2 = $k; for ($i = 0; $i < strlen($string); $i++) { if ($count2 == 0 && $string[$i] == 'T') {

PHP
View
generate functionThu, 06 Jul 2023

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order. timei is the time needed to prepare the order of the ith customer. When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input. Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.

function averageWaitTime($customers) { $time = 0; $prev = 0; $averageWaitTime = 0; for($i = 0; $i < count($customers); $i++) { // If there are other customers that arrive before the current one $customerArr = $customers[$i][0]; // The time needed to prepare the current order $customerTime = $customers[$i][1]; // If the current customer arrived first, he waits until the chef is done preparing the order of the previous customer if($customerArr < $prev) { $averageWaitTime += $prev - $customerArr; } // The time needed to prepare the current order $time = $customerArr + $customerTime; // The time the chef is done preparing the current order $prev = $time; } return $averageWaitTime / count($customers); }

PHP
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { public String numberToWords(int num) { if(num == 0) return "Zero"; String[] units = new String[] {"", "Thousand", "Million", "Billion"}; StringBuilder sb = new StringBuilder(); int i = -1; while(num > 0) { if(num % 1000 != 0) { StringBuilder tmp = new StringBuilder(); helper(num % 1000, tmp); tmp.append(units[++i]); sb.insert(0, tmp.toString()); } num /= 1000; } return sb.toString().trim(); } private void helper(int num, StringBuilder sb) { String[] lessThan20 = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "

Java
View
generate functionThu, 06 Jul 2023

Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) words = helper(num % 1000) +THOUSANDS[i] + " " + words; num /= 1000

Java
View

Want to kickstart your project?Use the new AI Studio to create your code