2019-02-10 Daily Challenge

What I've done today is Permuted multiples in Rust and Find Minimum in Rotated Sorted Array II in JavaScript.

Math

Problem

Permuted multiples

Problem 52

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

Solution

I've tried to find some graceful solution which need no stack-based array to determine if two numbers are permutation of each other, and get nothing.

So just brute force.

Implementation

fn main() {
    let mut down_bound = 100;
    let mut up_bound = 1000/6 + 1;
    let mut found: bool = false;
    let mut ans = 0;
    while !found {
        for i in down_bound..up_bound {
            let mut is_ok = true;
            for j in 2..7 {
                if !is_permutation(i, i*j) {
                    is_ok = false;
                    break;
                }
            }
            if is_ok {
                ans = i;
                found = true;
            }
        }
        down_bound *= 10;
        up_bound = down_bound*10/6 + 1;
    }
    println!("Answer is {}", ans);
}

fn is_permutation(mut u: usize, mut v: usize) -> bool {
    let mut a1: [usize; 10] = [0; 10];
    let mut a2: [usize; 10] = [0; 10];
    while u > 0 {
        a1[u%10] += 1;
        a2[v%10] += 1;
        u /= 10;
        v /= 10;
    }
    for i in 0..10 {
        if a1[i] != a2[i] {
            return false;
        }
    }
    true
}

Algorithm

Problem

154. Find Minimum in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

The array may contain duplicates.

Example 1:

Input: [1,3,5]
Output: 1

Example 2:

Input: [2,2,2,0,1]
Output: 0

Note:

Solution

I guess worst case of time complexity of algorithms will be $O(N)$, because we can't use binary partition to deal with duplicates.

Implementation

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
  if (nums[0] < nums[nums.length-1]) return nums[0];
  for (const val of nums) {
    if (val < nums[0]) return val;
  }
  return nums[0];
};