算法 - Random Flip Matrix

Random Flip Matrix 

You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all values are initially 0. Write a function flip which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.id, col.id] of that value. Also, write a function reset which sets all values back to 0. Try to minimize the number of calls to system's Math.random() and optimize the time and space complexity.

Note:

  1. 1 <= n_rows, n_cols <= 10000
  2. 0 <= row.id < n_rows and 0 <= col.id < n_cols
  3. flip will not be called when the matrix has no 0 values left.
  4. the total number of calls to flip and reset will not exceed 1000.

Example 1:

Input: 
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]]
Output: [null,[0,1],[1,2],[1,0],[1,1]]

Example 2:

Input: 
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
Output: [null,[0,0],[0,1],null,[0,0]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has two arguments, n_rows and n_colsflip and resethave no arguments. Arguments are always wrapped with a list, even if there aren't any.

Solution:

定义一个二维矩阵,随机翻转矩阵中的某一位,被选中翻转的位置概率满足均匀分布。

思路:

  • 将二维数组拉平成一维数组,随机用rand()%n 函数生成一个随机在[0,n)之内的值
  • 一维数组从后向前,将上面的值与从最后一位可“交换”的位关联,下一次随机到该值时,对应到关联位
  • 每一轮n--,随机区间变小,但是满足均匀分布
#include <vector>
#include <string>
#include <map>
#include <tr1/unordered_map>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace std::tr1;

int cnt = 5;
int rows = cnt;
int cols = cnt;
unordered_map<int,int> res;
int total = rows * cols;

void flip()
{
    vector<int> tmp, ret;
    int loop = total;
    for(int i = 0; i < loop; i++) {
        int val = rand() % total--; //每次递减

        int r = val / cols , c = val % cols;

        cout << setw(3) << i << "-th: val: " << val;
        if(res.find(val) == res.end()) {
            cout << ", (" << r  << "," << c << ")" << ", res[" << val << "] = " << total << endl;
        }
        else {
            int tmp = val;
            //注意:下面这个while是最容易忽略的点:有可能 res内部的第一个value已经被占用
            while ( res.find(res.find(val)->second) != res.end()) {
                val = res.find(val)->second;
            }

            cout << ", (" <<  res.find(val)->second / cols  << "," << res.find(val)->second % cols << ") , res[" << tmp << "] =" << total << ", old value: " << res.find(val)->second <<endl;
        }
        res[val] = total;
    }
}


void reset()
{
    res.clear();
    total = rows * cols;
}

int main()
{
    flip();
    reset();
    return 0;
}

结果:

  0-th: val: 8, (1,3), res[8] = 24
  1-th: val: 22, (4,2), res[22] = 23
  2-th: val: 6, (1,1), res[6] = 22
  3-th: val: 13, (2,3), res[13] = 21
  4-th: val: 8, (4,4) , res[8] =20, old value: 24
  5-th: val: 15, (3,0), res[15] = 19
  6-th: val: 10, (2,0), res[10] = 18
  7-th: val: 12, (2,2), res[12] = 17
  8-th: val: 11, (2,1), res[11] = 16
  9-th: val: 13, (4,1) , res[13] =15, old value: 21
 10-th: val: 2, (0,2), res[2] = 14
 11-th: val: 5, (1,0), res[5] = 13
 12-th: val: 0, (0,0), res[0] = 12
 13-th: val: 7, (1,2), res[7] = 11
 14-th: val: 7, (3,1) , res[7] =10, old value: 16
 15-th: val: 6, (4,3) , res[6] =9, old value: 23
 16-th: val: 6, (1,4) , res[6] =8, old value: 9
 17-th: val: 2, (2,4) , res[2] =7, old value: 14
 18-th: val: 2, (3,3) , res[2] =6, old value: 18
 19-th: val: 4, (0,4), res[4] = 5
 20-th: val: 1, (0,1), res[1] = 4
 21-th: val: 0, (3,2) , res[0] =3, old value: 17
 22-th: val: 0, (0,3) , res[0] =2, old value: 3
 23-th: val: 1, (3,4) , res[1] =1, old value: 19
 24-th: val: 0, (4,0) , res[0] =0, old value: 20

小注:

setw(int n)只是对直接跟在<<后的输出数据起作用,而在之后的<<需要在之前再一次使用setw来设定(Sets the number of characters to be used as the field width for the next insertion operation.)

n是在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中右对齐输出,可以使用setiosflags(ios::left)设置为左对齐输出,可以使用setfill('char x')使用x来填充空下的空格;

#include <iostream>
#include <iomanip> // setw所在的头文件
using namespace std;

void main(){
    cout << setw(10) << setiosflags(ios::left) << setfill('*') << 10 << endl;
    cout << setw(10) << setiosflags(ios::right) << setfill('*') << 10 << endl;
    cout << setw(10) << setfill('*') << 10 << endl;
}

比较清晰简洁的方式: 

int a = 100;
int b = 200;
cout.setf(ios::right, ios::adjustfield);
cout.fill('0');
cout << setw(5) << a <<endl;
cout << setw(5) << b << endl;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值