【HackerRank】Ice Cream Parlor
Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items whose sum equals M. The cost of a flavor (ci) will be no more than 10000.
Input Format
The first line of the input contains T, T test cases follow.
Each test case follows the format: The first line contains M. The second line contains the number N. The third line contains N single space separated integers denoting the price of each flavor ci.
Output Format
Output two integers, each of which is a valid index of the flavor. The lower index must be printed first. Indices are indexed from 1 to N.
Constraints
1 ≤ T ≤ 50
2 ≤ M ≤ 10000
2 ≤ N ≤ 10000
1 ≤ ci ≤ 10000
The prices of two items may be same and each test case has a unique solution.
又是一个求数组中两个数和正好是m的问题,类似leetcode中的Two Sum问题。
只是在这个问题中,数组中的数是有可能重复的,所以map中的value要用一个arrayList保存。
另外注意找的时候要保持i比在map中找到的坐标小(如代码26行的判断所示),以免重复。
代码如下:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- >0){
int m = in.nextInt();
int n = in.nextInt();
int[] prices = new int[n];
HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
for(int i = 0;i < n;i++){
prices[i]=in.nextInt();
if(!map.containsKey(prices[i]))
map.put(prices[i], new ArrayList<Integer>());
map.get(prices[i]).add(i);
}
for(int i = 0;i < n;i++){
int has = prices[i];
int toFind = m-prices[i];
if(map.containsKey(toFind)){
for(int temp:map.get(toFind)){
if(i < temp){
System.out.printf("%d %d",i+1,temp+1);
System.out.println();
}
}
}
}
}
}
}
【HackerRank】Ice Cream Parlor的更多相关文章
- HackerRank Ice Cream Parlor
传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
随机推荐
- Python 安装 MaxMind GeoLite City
1.先安装 geoip c library geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed ...
- Linux 文件管理(系统函数)
//read函数 #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* STDIN_FILEN ...
- eclipse建maven pom报错
pom报:org.apache.maven.archiver.MavenArchiver.getManifest 解决办法: 升级m2eclipse插件版本 help->install news ...
- linux 下Shell编程(四)
for循环应用实例 for 循环可以对一个记录集中地数据依次集中地数据依次进行处理. #!/bin/bash #4.13.sh for clear #看下图区别 for((i=1:i<1 ...
- HTML学习笔记——标准网页设计+使用CSS、Javascript
一.标准网页设计 1.标准网页概述: 标准网页设计要遵循,内容与表现相分离. 内容 + 表现 = 页面 --- 即 :XHTML + CSS = PAGE 内容与变现相分离,也就是内容使用HT ...
- splay tree旋转操作 hdu 1890
很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...
- poj3481(splay tree 入门题)
平衡树都能做. // // main.cpp // splay // // Created by 陈加寿 on 16/3/25. // Copyright © 2016年 chenhuan001. A ...
- BZOJ 2431
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1521 Solved: 883[Submit][Statu ...
- 【BZOJ2199】[Usaco2011 Jan]奶牛议会 2-SAT
[BZOJ2199][Usaco2011 Jan]奶牛议会 Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要 ...
- 160817、Java数据类型以及变量的定义
Java 是一种强类型的语言,声明变量时必须指明数据类型.变量(variable)的值占据一定的内存空间.不同类型的变量占据不同的大小. Java中共有8种基本数据类型,包括4 种整型.2 种浮点型. ...