博客
关于我
Weekly Contest 133
阅读量:423 次
发布时间:2019-03-06

本文共 7511 字,大约阅读时间需要 25 分钟。

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 = 0Output: [[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 = 1Output: [[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 = 2Output: [[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

 

Approach #1: 

class Solution {public:    vector
> allCellsDistOrder(int R, int C, int r0, int c0) { map
>> m; for (int i = 0; i < R; ++i) { for (int j = 0; j < C; ++j) { int dis = abs(i - r0) + abs(j - c0); m[dis].push_back({i, j}); } } int index = 0; vector
> ret = vector(R*C, vector
(2)); map
>>::iterator it; for (it = m.begin(); it != m.end(); ++it) { vector
> temp = it->second; for (int i = 0; i < temp.size(); ++i) { ret[index][0] = temp[i].first; ret[index][1] = temp[i].second; index++; } } return ret; }};

  

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 N people arrive in each city.

 

Example 1:

Input: [[10,20],[30,200],[400,50],[30,20]]Output: 110Explanation: 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

 

Approach #1: 

class Solution {    public int twoCitySchedCost(int[][] costs) {        int N = costs.length / 2;        int[][] dp = new int[N+1][N+1];        for (int i = 1; i <= N; ++i) {            dp[i][0] = dp[i-1][0] + costs[i-1][0];        }        for (int i = 1; i <= N; ++i) {            dp[0][i] = dp[0][i-1] + costs[i-1][1];        }        for (int i = 1; i <= N; ++i) {            for (int j = 1; j <= N; ++j) {                dp[i][j] = Math.min(dp[i-1][j] + costs[i+j-1][0], dp[i][j-1] + costs[i+j-1][1]);            }        }        return dp[N][N];    }}

  

1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.)

Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either:

  • 0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
  • 0 <= j < j + M - 1 < i < i + L - 1 < A.length.

 

Example 1:

Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2Output: 20Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

Example 2:

Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2Output: 29Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Example 3:

Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3Output: 31Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.

 

Note:

  1. L >= 1
  2. M >= 1
  3. L + M <= A.length <= 1000
  4. 0 <= A[i] <= 1000

 

Approach #1:

class Solution {    public int maxSumTwoNoOverlap(int[] A, int L, int M) {        for (int i = 1; i < A.length; ++i) {            A[i] += A[i-1];        }        int res = A[L+M-1], Lmax = A[L-1], Mmax = A[M-1];        for (int i = L + M; i < A.length; ++i) {            Lmax = Math.max(Lmax, A[i-M] - A[i-M-L]);            Mmax = Math.max(Mmax, A[i-L] - A[i-M-L]);            res = Math.max(res, Math.max(Lmax + A[i] - A[i-M], Mmax + A[i] - A[i-L]));        }                return res;    }}

  

1032. Stream of Characters

Implement the StreamChecker class as follows:

  • StreamChecker(words): Constructor, init the data structure with the given words.
  • query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.

 

Example:

StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.streamChecker.query('a');          // return falsestreamChecker.query('b');          // return falsestreamChecker.query('c');          // return falsestreamChecker.query('d');          // return true, because 'cd' is in the wordliststreamChecker.query('e');          // return falsestreamChecker.query('f');          // return true, because 'f' is in the wordliststreamChecker.query('g');          // return falsestreamChecker.query('h');          // return falsestreamChecker.query('i');          // return falsestreamChecker.query('j');          // return falsestreamChecker.query('k');          // return falsestreamChecker.query('l');          // return true, because 'kl' is in the wordlist

 

Note:

  • 1 <= words.length <= 2000
  • 1 <= words[i].length <= 2000
  • Words will only consist of lowercase English letters.
  • Queries will only consist of lowercase English letters.
  • The number of queries is at most 40000.

 

Approach #1:

class StreamChecker {    public class TriNode {        boolean isEnd = false;        TriNode[] next = new TriNode[26];    }        TriNode root = new TriNode();    StringBuilder buf = new StringBuilder();        void insert(String word) {        TriNode temp = root;        for (int i = 0; i < word.length(); ++i) {            char ch = word.charAt(word.length()-i-1);            if (temp.next[ch-'a'] == null) temp.next[ch-'a'] = new TriNode();            temp = temp.next[ch-'a'];        }        temp.isEnd = true;    }        public StreamChecker(String[] words) {        for (String word : words) {            insert(word);        }    }        public boolean query(char letter) {        buf.append(letter);        // System.out.println(buf.toString());        TriNode p = root;        for (int i = buf.length() - 1; i >= 0; --i) {            char ch = buf.charAt(i);            p = p.next[ch-'a'];            if (p == null) return false;            if (p.isEnd) return true;        }        return false;    }}/** * Your StreamChecker object will be instantiated and called as such: * StreamChecker obj = new StreamChecker(words); * boolean param_1 = obj.query(letter); */

  

 

转载地址:http://iftuz.baihongyu.com/

你可能感兴趣的文章
Delphi 10.3 Rio的RadioGroup1控件如何设置 Items 的排列为横向横排水平显示
查看>>
Delphi 10.3 应用程序获取自身所在的目录文件夹名称
查看>>
Delphi SQL 查询数据表中规定的时间段内按天统计出每天的记录数
查看>>
从Android JAR文件创建Delphi接口的第三方工具
查看>>
Python学习笔记
查看>>
Kotlin实现冒泡排序
查看>>
C#控制台冒泡程序
查看>>
NodeJS下TypeScript环境安装
查看>>
论如何找tensorflow的源码
查看>>
Promise封装ajax请求
查看>>
修改Promise对象的状态的方式
查看>>
使用zookeeper API实现zookeeper的基本操作
查看>>
APP开发,公司需要具备哪些条件才能成为佼佼者?
查看>>
汽车后市场,小程序为何独占鳌头
查看>>
宠物行业蓝海,APP如何突出重围?
查看>>
短视频小程序,互联网新风口
查看>>
彻底弄懂Python标准库源码(一)—— os模块
查看>>
实用软件推荐(一)——自动更换壁纸 (Dynamic theme)
查看>>
从零开始免费搭建自己的博客(七)——迁移 CSDN 博客到个人博客站点
查看>>
RF新手常见问题总结--(基础篇)
查看>>