189. Rotate Array

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input:

[1,2,3,4,5,6,7] and

k

 = 3

Output:

[5,6,7,1,2,3,4]

Explanation:

rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input:

[-1,-100,3,99] and

k

 = 2

Output:

 [3,99,-1,-100]

Explanation:

 rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

 

In place rotate

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return NULL
     */
    function rotate(&$nums, $k) {
        $c = count($nums);
        $steps = 0;
        $start = 0;
        
        while($steps < $c){  
            $i = $start;
            $prev = $nums[$i];
            do{         
                $next = ($i+$k)%$c;
                $temp = $nums[$next];
                $nums[$next] = $prev;
                $prev = $temp;
                $i += $k;
                $i = $i % $c;
                $steps++;
            }while($start != $i);
            $start ++;
            
        }
    
    }
}

 

917. Reverse Only Letters

Given a string S, return the “reversed” string where all characters that are not a letter stay in the same place, and all letters reverse their positions.

 

Example 1:

Input:

"ab-cd"

Output:

"dc-ba"

Example 2:

Input:

"a-bC-dEf-ghIj"

Output:

"j-Ih-gfE-dCba"

Example 3:

Input:

"Test1ng-Leet=code-Q!"

Output:

"Qedo1ct-eeLg=ntse-T!"

 

Note:

  1. S.length <= 100
  2. 33 <= S[i].ASCIIcode <= 122
  3. S doesn’t contain \ or "

 

Straightforward solution. 

  1. collect letters in reverse order.
  2. go through string from beginning, if it’s letter, replace with letter from reverse string, else keep special chars and adjust pointer accordingly
  3. of course there’s space for improvement, but not necessary
<?php
class Solution {

    /**
     * @param String $S
     * @return String
     */
    function reverseOnlyLetters($S) {
        $x = '';
        $y = '';
        for($i = strlen($S)-1; $i >=0; $i -- ){
            if(ctype_alpha($S[$i])){
                $x .= $S[$i];
            }
        }
        $xi = 0;
        for($i = 0; $i < strlen($S); $i ++){
            if(!ctype_alpha($S[$i])){
                $y .= $S[$i];
            } else {
                $y .= $x[$xi];
                $xi ++;
            }
             
        }
        
        return $y;
    }
}

?>

 

7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input:

 123

Output:

 321

Example 2:

Input:

 -123

Output:

 -321

Example 3:

Input:

 120

Output:

 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution {

    /**
     * @param Integer $x
     * @return Integer
     */
    function reverse($x) {
        
        $r =  (int) (strrev(trim($x,"+-")) * ($x>0?1:-1));
        
        if($r >  pow(2,31)-1 || $r < -pow(2,31)){
            return 0;
        }
        return $r;
    }
}

 

9. Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input:

 121

Output:

 true

Example 2:

Input:

 -121

Output:

 false

Explanation:

 From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input:

 10

Output:

 false

Explanation:

 Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

 

class Solution {

    /**
     * @param Integer $x
     * @return Boolean
     */
    function isPalindrome($x) {
        if($x < 0){
            return false;
        }
        return strrev($x) == $x;
    }
}

For the followup

not converting to string? Create a  stack of integers instead (like a push down automata)

26. Remove Duplicates from Sorted Array


Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn’t matter what you leave beyond the returned length.
Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn’t matter what values are set beyond the returned length.
Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}


class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function removeDuplicates(&$nums) {
        if(empty($nums)){
            return 0;
        }
        $i = 0;
        $r = 1;
        while($i < count($nums)-1){
            if($nums[$i] != $nums[$i+1]){
                $nums[$r] = $nums[$i+1];
                $r ++;
                
            }
            $i ++;
        }
        return $r;
    }
}

 

1030. Matrix Cells in Distance Order

We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.

Additionally, we are given a cell in that matrix with coordinates (r0, c0).

Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance.  Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|.  (You may return the answer in any order that satisfies this condition.)

 

Example 1:

Input:

R = 1, C = 2, r0 = 0, c0 = 0

Output:

[[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]

Example 2:

Input:

R = 2, C = 2, r0 = 0, c0 = 1

Output:

[[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

Input:

R = 2, C = 3, r0 = 1, c0 = 2

Output:

[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

 

Note:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C
class Solution {

    /**
     * @param Integer $R
     * @param Integer $C
     * @param Integer $r0
     * @param Integer $c0
     * @return Integer[][]
     */
    function allCellsDistOrder($R, $C, $r0, $c0) {

        $a =[];

        for($x = 0; $x < $R; $x ++){
           for($y = 0; $y < $C; $y ++){
             $a[] = [$x,$y];
           } 
        }
        usort($a, function($arr1, $arr2) use ($r0,$c0) {
            return (abs($arr1[0]-$r0)+ abs($arr1[1]-$c0) 
                    > 
                    abs($arr2[0]-$r0)+ abs($arr2[1]-$c0)
                   ) ? 1 : -1;
        });
        
        return $a;
        
    }
    

}

Thoughts:

  • For all easy level leetcode, just follow you heart, no need for advanced algorithms.
  • My solution:
    • Array Fill
    • Sort by  Manhattan distance
    • return

1029. Two City Scheduling

There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].

Return the minimum cost to fly every person to a city such that exactly Npeople arrive in each city.

 

Example 1:

Input:

[[10,20],[30,200],[400,50],[30,20]]

Output:

110

Explanation:

The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.

The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.

 

Note:

  1. 1 <= costs.length <= 100
  2. It is guaranteed that costs.length is even.
  3. 1 <= costs[i][0], costs[i][1] <= 1000

 

class Solution {

    /**
     * @param Integer[][] $costs
     * @return Integer
     */
    function twoCitySchedCost($costs) {
        usort($costs, function($arr1, $arr2) {
            return (abs($arr1[1]-$arr1[0]) < abs($arr2[1]-$arr2[0]) ) ? 1 : -1;
        });

        $a = 0;
        $b = 0;
        $total = 0;
        foreach($costs as $c){
            if($c[0] < $c[1]){

                if($a < count($costs)/2){
                    $total += $c[0];
                    $a ++;
                } else {
                    $total += $c[1];
                    $b ++;
                }
               
            } else{
                if($b < count($costs)/2){
                    $total += $c[1];
                    $b ++;
                } else {
                    $total += $c[0];
                    $a ++;
                }
                
            } 
        } 

        return $total;
    }
}

16ms.

 

 

Thoughts:

  • This code was used for the contest, so it’s not well structured (in terms of redundancy)
  • The basic idea:
    • Sort by switching cost (price diff), DESC.
    • then for each person, assign to lower cost city until full.
      • in case of full, assign to city with higher cost
    • Since sorted by price diff, at the time one city is full,  the cost to switch remaining people to the second choice is the lowest

If I’m a interviewer:

  • Let’s try three cities….
  • What about the capacity of each city is specified?
  • And what about the capacity is flexible and within a range provided?

1021. Remove Outermost Parentheses

A valid parentheses string is either empty ("")"(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.  For example, """()""(())()", and "(()(()))" are all valid parentheses strings.

A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.

Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.

Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.

 

Example 1:

Input:

"(()())(())"

Output:

"()()()"

Explanation:

The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".

Example 2:

Input:

"(()())(())(()(()))"

Output:

"()()()()(())"

Explanation:

The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".

Example 3:

Input:

"()()"

Output:

""

Explanation:

The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".

 

Note:

  1. S.length <= 10000
  2. S[i] is "(" or ")"
  3. S is a valid parentheses string

class Solution {
    public String removeOuterParentheses(String S) {
        int lv = 0;
        String result = "";
        
        for(char c : S.toCharArray()){
            if(c == '('){
                if(lv > 0){
                    result += c;
                }
                lv ++;
                
            } else if(c == ')'){
                lv--;
                if(lv > 0){
                    result += c;
                }
            }
        }
        return result;
    }
}

 

* Using a StringBuilder will somehow improve the performance.

Thoughts:

Since we can assume the input is valid, just use a counter to mark the depth. Send char to result only when depth >= 1

If I’m a Interviewer

  1. What if the input string is not always valid?
    • ref: LC20
  2. What if we upgrade parentheses to brackets ()[]{} ?
    • Use Stack and Stack.size() instead of level counter
  3. What if we remove inner most (max depth) parentheses instead?
    • Go through twice, 1) find max depth, 2) copy to result and skip max depth.

Well we can always construct an array of ints (levels) as a form of representation.

For example, “(()())(())(()(()))” -> [1,2,2,1,2,1,2,2,3]

Then we can easily remove any levels (outer/inner/somewhere in the middle).

  • Remove Outer: remove 1, everything else —
  • Remove Inner: remove max
  • Remove Level2: remove 2, everything > 2 decrease by 1.